Quick Answer

Implement concurrent execution.

Understanding the Issue

Modern C++ provides thread support through the <thread> header, with synchronization primitives like mutexes and condition variables.

The Problem

This code demonstrates the issue:

Cpp Error
// Need to execute tasks concurrently

The Solution

Here's the corrected code:

Cpp Fixed
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>

std::mutex cout_mutex;

void worker(int id) {
    std::lock_guard<std::mutex> lock(cout_mutex);
    std::cout << "Thread " << id << " working
";
}

int main() {
    std::vector<std::thread> threads;
    
    // Launch threads
    for (int i = 0; i < 5; ++i) {
        threads.emplace_back(worker, i);
    }
    
    // Join threads
    for (auto& t : threads) {
        t.join();
    }
    
    return 0;
}

Key Takeaways

Use std::thread with proper synchronization for concurrent tasks.