Quick Answer

Using <thread> and async for concurrent execution.

Understanding the Issue

C++11+ provides standardized threading with threads, futures, and synchronization primitives. Requires careful handling of shared data.

The Problem

This code demonstrates the issue:

Cpp Error
// Problem: Data race
int counter = 0;
auto f = []{ counter++; };
thread t1(f), t2(f); // Undefined behavior

The Solution

Here's the corrected code:

Cpp Fixed
// Solution: mutex protection
mutex m;
auto safe_f = [&]{
    lock_guard<mutex> lg(m);
    counter++;
};

Key Takeaways

Always synchronize access to shared resources.