33 lines · cpp
1#include <chrono>2#include <thread>3 4void5thread_function ()6{7 // Set thread-specific breakpoint here.8 std::this_thread::sleep_for(std::chrono::milliseconds(20));9 // On Windows, a sleep_for of less than about 16 ms effectively calls10 // Sleep(0). The MS standard thread implementation uses a system thread11 // pool, which can deadlock on a Sleep(0), hanging not only the secondary12 // thread but the entire test. I increased the delay to 20 ms to ensure13 // Sleep is called with a delay greater than 0. The deadlock potential14 // is described here:15 // https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep#remarks16}17 18int 19main ()20{21 // Set main breakpoint here.22 23 #ifdef __APPLE__24 pthread_setname_np("main-thread");25 #endif26 27 std::thread t(thread_function);28 t.join();29 30 thread_function();31 return 0;32}33