thread 利用event顺序同步 (windows )
// MultiThreadTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <process.h> #include <windows.h> HANDLE g_hEvent; HANDLE g_hEvent1; HANDLE g_hEvent2; HANDLE g_hEvent3; unsigned __stdcall WordCount(void *pvParam); unsigned __stdcall SpellCheck(void *pvParam); unsigned __stdcall GrammarCheck(void *pvParam); int _tmain(int argc, _TCHAR* argv[]) { g_hEvent1 = CreateEvent(NULL, FALSE, FALSE, NULL); g_hEvent2 = CreateEvent(NULL, FALSE, FALSE, NULL); g_hEvent3 = CreateEvent(NULL, FALSE, FALSE, NULL); HANDLE hThread[3]; unsigned dwThreadID[3]; hThread[0] = (HANDLE)_beginthreadex(NULL, 0, WordCount, NULL, 0, &dwThreadID[0]); hThread[1] = (HANDLE)_beginthreadex(NULL, 0, SpellCheck, NULL, 0, &dwThreadID[1]); hThread[2] = (HANDLE)_beginthreadex(NULL, 0, GrammarCheck, NULL, 0, &dwThreadID[2]); SetEvent(g_hEvent1); DWORD dwCThd = WaitForMultipleObjects (3, //count of objects hThread, //thread handle TRUE, //wait for all INFINITE); //time out interval printf("main thread exit\n"); return 0; } unsigned __stdcall WordCount(void *pvParam) { int i =0; while(i<5) { WaitForSingleObject(g_hEvent1, INFINITE); ResetEvent(g_hEvent1);//lock printf("enter func===1\n "); SetEvent(g_hEvent2);//unlock i++; } return(0); } unsigned __stdcall SpellCheck(void *pvParam) { int i =0; while(i<5) { WaitForSingleObject(g_hEvent2, INFINITE); ResetEvent(g_hEvent2); printf("enter func===2 \n"); SetEvent(g_hEvent3); i++; } return(0); } unsigned __stdcall GrammarCheck(void *pvParam) { int i = 0; while(i<5) { WaitForSingleObject(g_hEvent3, INFINITE); ResetEvent(g_hEvent3); printf("enter func===3 \n"); SetEvent(g_hEvent1); i++; } return(0); }
参照如下地址,自己写了一遍,理解下event。
//http://blog.csdn.net/morewindows/article/details/7445233
//http://2309998.blog.51cto.com/2299998/1263928
#include<windows.h> #include<iostream> using namespace std; #pragma argsused //子线程事件 HANDLE g_ThreadEvent[3]; int g_EventIndex = 0; DWORD WINAPI ThreadFunc(void *p) { int param=(int)p; char c='A'+param; for(int iIndex =0; iIndex < 10; iIndex ++) { //线程ABC分别等待事件0,1,2 WaitForSingleObject(g_ThreadEvent[param], INFINITE); printf("the thread is %c\n",c); ResetEvent(g_ThreadEvent[g_EventIndex]);// 重置 挂起 当前event ,Sets the specified event object to the nonsignaled state.https://msdn.microsoft.com/en-us/library/windows/desktop/ms685081%28v=vs.85%29.aspx
g_EventIndex = (g_EventIndex+1)%3; SetEvent(g_ThreadEvent[g_EventIndex]);//通知 激活下一个 event, Sets the specified event object to the signaled state. https://msdn.microsoft.com/en-us/library/windows/desktop/ms686211%28v=vs.85%29.aspx } return 0; } int main(int argc, char* argv[]) { //全为自动模式一次只能进去一个 for(int iIndex = 0; iIndex < 3; iIndex++) { g_ThreadEvent[iIndex] = CreateEvent(NULL, false, false, NULL); } SetEvent(g_ThreadEvent[0]); HANDLE hThread[3]; for(int iIndex = 0; iIndex < 3; iIndex++) { hThread[iIndex] = (HANDLE)CreateThread(NULL, 0,ThreadFunc, (void *)iIndex, 0, NULL); } //等待所有线程结束 WaitForMultipleObjects(3, hThread, TRUE, INFINITE); cout<<"运行结束,按任意键退出.....\n"; char c= getchar(); CloseHandle(g_ThreadEvent); return 0; } }
-------------------
后话20191010
c++ 11 thread 库,已经足够好, 推荐用c++ 内置thread,简单好用. (API 设计真的是一门艺术)
https://zh.cppreference.com/w/cpp
Thread support library (C++11) https://en.cppreference.com/w/cpp/thread