#include #include #include // Code to be run by the additional threads. void thread_code(int id) { std::cout << "Hello World from thread " << id << std::endl; } int main(int, char *[]) { int constexpr NUM_THREADS = 4; // A vector to keep the thread objects. std::vector threads; // Create the threads and put their objects in-place in the vector. for (int i = 0; i < NUM_THREADS; ++i) { threads.emplace_back(thread_code, i); } // Main thread continues. std::cout << "Now there are " << NUM_THREADS + 1 << " threads running.\n"; // Wait for all additional threads to finish. for (auto &thr : threads) { thr.join(); } std::cout << "Now there is only one thread." << std::endl; return 0; }