使用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;
}

  

posted on 2020-10-09 15:25  lialin  阅读(360)  评论(0编辑  收藏  举报

导航