C++多线程同步技巧(二)--- 事件
简介
Windows在线程控制方面提供了多种信号处理机制,其中一种便是使用 CreateEvent() 函数创建事件,然后使用信号控制线程运行。其中将事件变为有信号可使用 SetEvent() 函数,将事件信号复位(变为无信号)可使用 ResetEvent() 函数,信号可以配合 WaitForSingleObject() 函数对线程的同步进行控制,当有信号时,此函数便会放行;无信号时,此函数会将阻塞。
提示: CreateEvent() 函数的参数 bManualReset 的含义是信号是否由人工复位,如果选择true,则信号必须手动采用ResetEvent() 函数进行复位操作。在这种情况下,可能会偶尔出现线程不同步的情况,问题出在可能同时会有多个线程穿过 WaitForSingleObject() 函数,导致复位失效,所以在这种情况下,为确保万无一失,我们一般会再添加一个限制条件,例如临界区或互斥体;如果选择的是false,则当一个信号经过 WaitForSingleObject() 函数的时候,函数会自动将事件信号复位。
代码样例
- bManualReset参数为 false
////////////////////////////////
//
// FileName : ThreadEventDemo.cpp
// Creator : PeterZheng
// Date : 2018/9/23 18:00
// Comment : The usage of "CreateEvent"
//
////////////////////////////////
#pragma once
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
DWORD WINAPI func1(LPVOID lpParam);
DWORD WINAPI func2(LPVOID lpParam);
HANDLE hEvent = NULL;
unsigned int unCount = 0;
DWORD WINAPI func1(LPVOID lpParam)
{
while (true)
{
WaitForSingleObject(hEvent, INFINITE);
if (unCount < 100)
{
unCount++;
Sleep(10);
cout << "Count: " << unCount << endl;
SetEvent(hEvent);
continue;
}
// 因为WaitForSingleObject函数会自动复位,可能导致另外一个线程始终等待不到信号,造成“假死”现象,所以这里需要使用SetEvent重置信号。
SetEvent(hEvent);
break;
}
return 0;
}
DWORD WINAPI func2(LPVOID lpParam)
{
while (true)
{
WaitForSingleObject(hEvent, INFINITE);
if (unCount < 100)
{
unCount++;
Sleep(10);
cout << "Count: " << unCount << endl;
SetEvent(hEvent); // 设置事件为有信号状态
continue;
}
SetEvent(hEvent);
break;
}
return 0;
}
int main(void)
{
HANDLE hThread[2] = { NULL };
hEvent = CreateEvent(NULL, false, false, NULL); //创建一个匿名事件,当参数bManualReset设置为false时
hThread[0] = CreateThread(NULL, 0, func1, NULL, 0, NULL);
cout << "Thread-1 is RUNNING" << endl;
hThread[1] = CreateThread(NULL, 0, func2, NULL, 0, NULL);
cout << "Thread-2 is RUNNING" << endl;
SetEvent(hEvent);
WaitForMultipleObjects(2, hThread, true, INFINITE); //等待两个线程运行结束
CloseHandle(hThread[0]);
CloseHandle(hThread[1]);
CloseHandle(hEvent);
system("pause");
return 0;
}
- bManualReset参数为 true
////////////////////////////////
//
// FileName : ThreadEventDemo.cpp
// Creator : PeterZheng
// Date : 2018/9/23 18:00
// Comment : The usage of "CreateEvent"
//
////////////////////////////////
#pragma once
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
DWORD WINAPI func1(LPVOID lpParam);
DWORD WINAPI func2(LPVOID lpParam);
HANDLE hEvent = NULL;
HANDLE hMutex = NULL;
unsigned int unCount = 0;
DWORD WINAPI func1(LPVOID lpParam)
{
while (true)
{
WaitForSingleObject(hEvent, INFINITE);
WaitForSingleObject(hMutex, INFINITE); //为互斥体上锁
ResetEvent(hEvent); // 重置事件为无信号状态
if (unCount < 100)
{
unCount++;
Sleep(10);
cout << "Count: " << unCount << endl;
SetEvent(hEvent); // 设置事件为有信号状态
ReleaseMutex(hMutex); //互斥体解锁
}
else
{
SetEvent(hEvent);
ReleaseMutex(hMutex);
break;
}
}
return 0;
}
DWORD WINAPI func2(LPVOID lpParam)
{
while (true)
{
WaitForSingleObject(hEvent, INFINITE);
WaitForSingleObject(hMutex, INFINITE); //为互斥体上锁
ResetEvent(hEvent); // 重置事件为无信号状态
if (unCount < 100)
{
unCount++;
Sleep(10);
cout << "Count: " << unCount << endl;
SetEvent(hEvent); // 设置事件为有信号状态
ReleaseMutex(hMutex);
}
else
{
SetEvent(hEvent);
ReleaseMutex(hMutex);
break;
}
}
return 0;
}
int main(void)
{
HANDLE hThread[2] = { NULL };
hEvent = CreateEvent(NULL, true, false, NULL); //创建一个匿名事件,当参数bManualReset设置为true时
hMutex = CreateMutex(NULL, false, NULL); //创建一个匿名互斥体
hThread[0] = CreateThread(NULL, 0, func1, NULL, 0, NULL);
cout << "Thread-1 is RUNNING" << endl;
hThread[1] = CreateThread(NULL, 0, func2, NULL, 0, NULL);
cout << "Thread-2 is RUNNING" << endl;
SetEvent(hEvent); // 设置事件为有信号状态
WaitForMultipleObjects(2, hThread, true, INFINITE); //等待两个线程运行结束
CloseHandle(hThread[0]);
CloseHandle(hThread[1]);
CloseHandle(hEvent);
CloseHandle(hMutex);
system("pause");
return 0;
}
参考文档
本文为博主总结文章,欢迎转载,请注明出处。