//事件内核对象例子(自动重置)
#include<iostream>
#include<Windows.h>
using namespace std;
unsigned int uInt=NULL;
unsigned int uIntTwo = NULL;
bool EventBool = true;
DWORD WINAPI EventFunction(LPVOID lpData) {
HANDLE handle=OpenEvent(EVENT_ALL_ACCESS,FALSE, (LPCWSTR)"EventDemo");
WaitForSingleObject(handle,INFINITE);
while (EventBool) {
uInt++;
uIntTwo++;
}
CloseHandle(handle);
return NULL;
}
int main() {
HANDLE handleEvent=CreateEvent(NULL, false,true,(LPCWSTR)"EventDemo");//创建事件内核
DWORD dwThreadId[2];
HANDLE handle[2];
handle[0]=CreateThread(0,0,EventFunction,0,CREATE_SUSPENDED,&dwThreadId[0]);
handle[1] = CreateThread(0, 0, EventFunction, 0, CREATE_SUSPENDED, &dwThreadId[1]);
ResumeThread(handle[0]);
ResumeThread(handle[1]);
Sleep(1000);
EventBool = false;
cout << "uINT:" << uInt << endl;
cout << "uIntTwo:" << uIntTwo << endl;
CloseHandle(handleEvent);
CloseHandle(handle[0]);
CloseHandle(handle[1]);
}
Windows事件内核使用(自动重置)例子
最新推荐文章于 2023-01-05 14:41:21 发布