#include // std::cout #include // std::atomic #include // std::thread #include // std::vector using namespace std; atomic atomic_counter (0); int unsync_counter = 0; void increase_atomic(int n) { for (int i=0; i threads; cout << "increase atomic counter with 10 threads...\n"; for (int i=1; i<=10; ++i) threads.push_back(thread(increase_atomic, 10000000)); cout << "increase unsynchronized counter with 10 threads...\n"; for (int i=1; i<=10; ++i) threads.push_back(thread(increase_unsync, 10000000)); cout << endl << "wait for all threads..." << endl; int joined = 0; for (auto& th : threads) { th.join(); joined++; } cout << "join'ed " << joined << " threads" << endl; cout << endl << "atomic counter: " << atomic_counter << '\n'; cout << "unsync counter: " << unsync_counter << '\n'; return 0; }