/* * simple synchronization example, version 1: * * unsynchronized access to a resource (here, standard output) can lead to * problems. * * command-line argument gives number of threads. */ #include #include #include #include void thread_fcn(int thread_id, int num_threads); /* ---- main program ---- */ int main(int argc, char* argv[]) { const char* usage_fmt = "usage: %s number_of_threads\n"; /* process command-line arguments */ char* end_ptr_for_strtol; if (argc != 2) { fprintf(stderr, usage_fmt, argv[0]); exit(EXIT_FAILURE); } int num_threads = strtol(argv[1], &end_ptr_for_strtol, 10); if (*end_ptr_for_strtol != '\0') { fprintf(stderr, usage_fmt, argv[0]); exit(EXIT_FAILURE); } std::cout << "hello from main program\n"; std::vector threads; /* create and start threads */ for (int i = 0; i < num_threads; ++i) { threads.push_back(std::thread(thread_fcn, i, num_threads)); } std::cout << "threads launched\n"; /* wait for threads to finish */ for (auto& t : threads) { t.join(); } std::cout << "threads all done\n"; return EXIT_SUCCESS; } /* ---- code to be executed by each thread ---- */ void thread_fcn(int thread_id, int num_threads) { std::cout << "hello from thread " << thread_id << " of " << num_threads << "\n"; }