CodeTheRobot

C++ Intermediate · exercise 06

Threads & synchronisation

Increment one counter from two threads without losing updates.

C++ Intermediate lesson 06

Guided steps

  1. 1.Protect the shared counter with a std::mutex and std::lock_guard.
  2. 2.Join both threads before reading the result.
  3. 3.The total must be exactly 2000 on every run: that is what a correct lock buys you.

Your code

Your code is saved in this browser after every run and restored next time.