使用WaitForSingleObject阻塞唤醒线程
#include <cstdio> #include <string> #include "windows.h" #include <thread> HANDLE g_hEvent; void TestThread() { ResetEvent(g_hEvent); printf("ResetEvent finish\n"); WaitForSingleObject(g_hEvent, INFINITE); printf("Test thread end\n"); } int main(void) { g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); ResetEvent(g_hEvent); std::thread t(TestThread); Sleep(5000); printf("Main thread notify test thread end\n"); SetEvent(g_hEvent); Sleep(1000); t.join(); printf("main thread end\n"); return 0; }