https://github.com/gongluck/Windows-Core-Program.gitc++
//第9章 用內核對象進行線程同步.cpp: 定義應用程序的入口點。 // #include "stdafx.h" #include "第9章 用內核對象進行線程同步.h" #include <Wct.h> DWORD WINAPI Thread(PVOID param) { Sleep(2000); return 0; } HANDLE g_event = nullptr; DWORD WINAPI Thread2(PVOID param) { WaitForSingleObject(g_event, INFINITE); //使事件未觸發 BOOL bres = ResetEvent(g_event); //處理事件 //... //觸發事件 bres = SetEvent(g_event); return 0; } HANDLE g_timer = nullptr; DWORD WINAPI Thread3(PVOID param) { WaitForSingleObject(g_timer, INFINITE); return 0; } int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { HANDLE hthread = CreateThread(nullptr, 0, Thread, nullptr, 0, nullptr); //等待線程被觸發 DWORD dres = WaitForSingleObject(hthread, INFINITE);//INFINITE等待無限長的時間 switch (dres) { case WAIT_OBJECT_0: //線程被觸發(終止) CloseHandle(hthread); hthread = nullptr; break; case WAIT_TIMEOUT: //超時 break; case WAIT_FAILED: //FAILED break; } HANDLE hthreads[3]; for (int i = 0; i < 3; ++i) hthreads[i] = CreateThread(nullptr, 0, Thread, nullptr, 0, nullptr); //等待多個線程被觸發 dres = WaitForMultipleObjects(3, hthreads, FALSE, INFINITE); switch (dres) { case WAIT_OBJECT_0+0: //線程被觸發(終止) CloseHandle(hthreads[0]); hthreads[0] = nullptr; break; case WAIT_OBJECT_0+1: //線程被觸發(終止) CloseHandle(hthreads[1]); hthreads[1] = nullptr; break; case WAIT_OBJECT_0+2: //線程被觸發(終止) CloseHandle(hthreads[2]); hthreads[2] = nullptr; break; case WAIT_TIMEOUT: //超時 break; case WAIT_FAILED: //FAILED break; } dres = WaitForMultipleObjects(3, hthreads, TRUE, INFINITE); for (int i = 0; i < 3; ++i) { if (hthreads[i] == nullptr) continue; CloseHandle(hthreads[i]); hthreads[i] = nullptr; } //事件 //OpenEvent打開已建立的事件 g_event = CreateEvent(nullptr, TRUE, FALSE, nullptr);//若是是自動重置事件,能讓等待線程獲得事件後立刻把事件置爲未觸發,形成同時只有一個線程獲得事件! HANDLE hthreads2[2]; for (int i = 0; i < 2; ++i) hthreads2[i] = CreateThread(nullptr, 0, Thread2, nullptr, 0, nullptr); SetEvent(g_event);//觸發事件 dres = WaitForMultipleObjects(2, hthreads2, TRUE, INFINITE); for (int i = 0; i < 2; ++i) { if (hthreads2[i] == nullptr) continue; CloseHandle(hthreads2[i]); hthreads2[i] = nullptr; } CloseHandle(g_event); g_event = nullptr; //可等待的計時器內核對象 //OpenWaitableTimer打開已建立的定時器 g_timer = CreateWaitableTimer(nullptr, TRUE, nullptr); LARGE_INTEGER li; li.QuadPart = -(2 * 10000000); SetWaitableTimer(g_timer, &li, 0, nullptr, nullptr, FALSE); HANDLE hthread3 = CreateThread(nullptr, 0, Thread3, nullptr, 0, nullptr); WaitForSingleObject(hthread3, INFINITE); CloseHandle(hthread3); hthread3 = nullptr; CancelWaitableTimer(g_timer); CloseHandle(g_timer); g_timer = nullptr; //信號量 //CreateSemaphore //OpenSemaphore //WaitForSingleObject //ReleaseSemaphore //互斥量 //CreateMutex //OpenMutex //WaitForSingleObject //ReleaseMutex //WaitForInputIdle //這對於父進程和子進程之間的同步是極其有用的,由於CreateProcess函數不會等待新進程完成它的初始化工做。 //在試圖與子線程通信前,父線程能夠使用WaitForInputIdle來判斷子線程是否已經初始化完成。 //MsgWaitForMultipleObjects //等候單個對象或一系列對象發出信號---標誌着規定的超時已通過去,或特定類型的消息已抵達線程的輸入隊列。如返回條件已經知足,則當即返回 //WaitForDebugEvent //獲取調試事件 //SignalObjectAndWait //觸發一個對象並等待另外一個對象 //等待鏈遍歷(WCT) //OpenThreadWaitChainSession //GetThreadWaitChain system("pause"); return 0; }