/* * "hello world" program with C++11 threads: * * command-line argument gives number of threads. */ #include #include #include #include void thread_fcn(int myID); /* ---- 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); } printf("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)); } printf("threads launched\n"); /* wait for threads to finish */ for (auto& t : threads) { t.join(); } printf("threads all done\n"); return EXIT_SUCCESS; } /* ---- code to be executed by each thread ---- */ void thread_fcn(int myID) { printf("hello from thread %d\n", myID); }