#include #include #include #include // Here we have a race condition on variable value. int main(int argc, char *argv[]) { if (argc != 2) { std::cerr << "Please give the desired number of threads" " in the command line.\n"; return 1; } int value; int const n_threads = std::stoi(argv[1]); if (n_threads <= 0) { std::cerr << "Number of threads must be positive.\n"; return 2; } std::vector threads; for (int i = 0; i < n_threads; ++i) { threads.emplace_back([&value](int id) { value = 2 * id; }, i); } for (auto &thd : threads) { thd.join(); } // Guess what will be printed. std::cout << "Value is " << value << std::endl; }