首页 技术 正文
技术 2022年11月15日
0 收藏 317 点赞 3,752 浏览 3480 个字

简介

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

参考文档

【1】https://blog.csdn.net/s_lisheng/article/details/74278765

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,966
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783