Arguments of Threads - Race Conditions and Locks
Both issues from the previous lesson are actually race conditions because the result of the program depends on the interleaving of the operations. The race condition is the cause of the data race.
Fixing the data race is quite easy;
valSleeper should be protected using either a lock or an atomic. To overcome the lifetime issues of
std::cout, you have to join the thread instead of detaching it.
Here is the modified main function.