std::thread first (function_to_run_in_thread);
1234567891011
void loop() { while(true){} } void main() { start loopThread1(loop); start loopThread2(loop); // program cannot halt after the start of threads }
123456789101112131415161718192021
#include <atomic> #include <thread> std::atomic<bool> exiting(false); void loop() { while (!exiting) ; } int main() { std::thread t1(loop); std::thread t2(loop); exiting = true; t1.join(); t2.join(); }