SETEVENT的使用
来源:https://msdn.microsoft.com/en-us/library/windows/desktop/ms686915(v=vs.85).aspx
昨天看到这个SetEvent的方法,感觉很新鲜。今天记录一下
The following example uses event objects to prevent several threads from reading from a shared memory buffer while a master thread is writing to that buffer. First, the master thread uses the CreateEvent function to create a manual-reset event object whose initial state is nonsignaled. Then it creates several reader threads. The master thread performs a write operation and then sets the event object to the signaled state when it has finished writing.
Before starting a read operation, each reader thread uses WaitForSingleObject to wait for the manual-reset event object to be signaled. When WaitForSingleObject returns, this indicates that the main thread is ready for it to begin its read operation.
#include <windows.h>
#include <stdio.h> #define THREADCOUNT 4 HANDLE ghWriteEvent;
HANDLE ghThreads[THREADCOUNT]; DWORD WINAPI ThreadProc(LPVOID); void CreateEventsAndThreads(void)
{
int i;
DWORD dwThreadID; // Create a manual-reset event object. The write thread sets this
// object to the signaled state when it finishes writing to a
// shared buffer. ghWriteEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual-reset event
FALSE, // initial state is nonsignaled
TEXT("WriteEvent") // object name
); if (ghWriteEvent == NULL)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return;
} // Create multiple threads to read from the buffer. for(i = ; i < THREADCOUNT; i++)
{
// TODO: More complex scenarios may require use of a parameter
// to the thread procedure, such as an event per thread to
// be used for synchronization.
ghThreads[i] = CreateThread(
NULL, // default security
, // default stack size
ThreadProc, // name of the thread function
NULL, // no thread parameters
, // default startup flags
&dwThreadID); if (ghThreads[i] == NULL)
{
printf("CreateThread failed (%d)\n", GetLastError());
return;
}
}
} void WriteToBuffer(VOID)
{
// TODO: Write to the shared buffer. printf("Main thread writing to the shared buffer...\n"); // Set ghWriteEvent to signaled if (! SetEvent(ghWriteEvent) )
{
printf("SetEvent failed (%d)\n", GetLastError());
return;
}
} void CloseEvents()
{
// Close all event handles (currently, only one global handle). CloseHandle(ghWriteEvent);
} int main( void )
{
DWORD dwWaitResult; // TODO: Create the shared buffer // Create events and THREADCOUNT threads to read from the buffer CreateEventsAndThreads(); // At this point, the reader threads have started and are most
// likely waiting for the global event to be signaled. However,
// it is safe to write to the buffer because the event is a
// manual-reset event. WriteToBuffer(); printf("Main thread waiting for threads to exit...\n"); // The handle for each thread is signaled when the thread is
// terminated.
dwWaitResult = WaitForMultipleObjects(
THREADCOUNT, // number of handles in array
ghThreads, // array of thread handles
TRUE, // wait until all are signaled
INFINITE); switch (dwWaitResult)
{
// All thread objects were signaled
case WAIT_OBJECT_0:
printf("All threads ended, cleaning up for application exit...\n");
break; // An error occurred
default:
printf("WaitForMultipleObjects failed (%d)\n", GetLastError());
return ;
} // Close the events to clean up CloseEvents(); return ;
} DWORD WINAPI ThreadProc(LPVOID lpParam)
{
// lpParam not used in this example.
UNREFERENCED_PARAMETER(lpParam); DWORD dwWaitResult; printf("Thread %d waiting for write event...\n", GetCurrentThreadId()); dwWaitResult = WaitForSingleObject(
ghWriteEvent, // event handle
INFINITE); // indefinite wait switch (dwWaitResult)
{
// Event object was signaled
case WAIT_OBJECT_0:
//
// TODO: Read from the shared buffer
//
printf("Thread %d reading from buffer\n",
GetCurrentThreadId());
break; // An error occurred
default:
printf("Wait error (%d)\n", GetLastError());
return ;
} // Now that we are done reading the buffer, we could use another
// event to signal that this thread is no longer reading. This
// example simply uses the thread handle for synchronization (the
// handle is signaled when the thread terminates.) printf("Thread %d exiting\n", GetCurrentThreadId());
return ;
}
CreateEvent,SetEvent,ResetEvent这三个方法主要是用于线程同步,和通信。
是否需要使用ResetEvent是根据CreateEvent的第二个参数而定的
TRUE, // manual-reset event。
SETEVENT的使用的更多相关文章
- C++多线程的几个重要方法解析CreateEvent / SetEvent /ResetEvent/ 等
1.CreateEvent 是创建windows事件的意思,作用主要用在判断线程退出,程锁定方面. 函功能描述:创建或打开一个命名的或无名的事件对象. HANDLE m_hExit; m_hExit= ...
- 事件函数SetEvent、PulseEvent与WaitForSingleObject详解
系统核心对象中的Event事件对象,在进程.线程间同步的时候是比较常用,发现它有两个出发函数,一个是SetEvent,还有一个PulseEvent, 两者的区别是: SetEvent为设置事件对象为有 ...
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法
首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面. CreateEvent 函功能描述:创建或打开一个命名的或无名的事件对象. EVENT有两种状态 ...
- SetEvent/ResetEvent
在自己主动重置事件对象中,当WaitSingleObject/WaitForMultipleObjects接收到SetEvent发送过来的信号后则返回WAIT_OBJECT_0,此时操作系统(待定)自 ...
- CreateEvent、SetEvent、ResetEvent和WaitForSingleObject
事件对象就像一个开关:它仅仅有两种状态---开和关.当一个事件处于"开"状态.我们称其为"有信号".否则称为"无信号". 能够在一个线程的运 ...
- c++中SetEvent和ResetEvent的使用
关于事件 事件(Event)是WIN32提供的最灵活的线程间同步方式,事件可以处于激发状态(signaled or true)或未激发状态(unsignal or false).根据状态变迁方式的不同 ...
- MyEvent.SetEvent; // 同步信号置位
MyEvent.SetEvent; // 同步信号置位 TSimpleEvent.Create = TEvent.Create(nil, True, False, nil) ...
- CreateEvent和SetEvent及WaitForSingleObject的使用方法
CreateEvent: 1.函数功能: 创建一个命名或匿名的事件对象 2.函数原型: HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttri ...
- Windows CreateEvent,SetEvent,WaitForSingleObject的用法
http://blog.pfan.cn/embed/19089.html WaitForSingleObject的用法 DWORD WaitForSingleObject( HANDLE hHan ...
随机推荐
- VS2008中编译通过,但调试时出现“未使用调试信息生成二进制文件”的问题
.只要是“建立项目的时候不应建立空项目,而应当建立一个“win32控制台应用程序”.这样确实可以解决问题.只要你选择的是这个"win32控制台应用程序"则在附加选项里面选不选上“空 ...
- hdu 5381 The sum of gcd(线段树+gcd)
题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...
- GIS可视化——热点格网图
一.简介 原理:按照格网大小将区域进行划分,由一个矩形格网替代当前范围内的数据,由格网中心数字代替格网的权重(可以为格网中数据的数量,数据某权重的平均值.最大值.最小值等), 由格网之间颜色的不同表达 ...
- from: Java开发必须要知道的知识体系
from: https://zhuanlan.zhihu.com/p/21895647 作者:靳洪飞链接:https://zhuanlan.zhihu.com/p/21895647来源:知乎著作权归 ...
- Node.app让Nodejs平台在iOS和OS X系统上奔跑
首先呢,欢迎大家去查看相同内容的链接:http://www.livyfeel.com/nodeapp/. 由于那个平台我用的markdown语法,我也懒得改动了,就这样黏贴过来了. 这是一个惊人的恐怖 ...
- js学习总结--DOM2兼容处理重复问题
在解决this问题之后,只需要在每次往自定义属性和事件池当中添加事件的时候进行以下判断就好了,具体代码如下: /* bind:处理DOM2级事件绑定的兼容性问题(绑定方法) @parameter: c ...
- Mysql或者Hive数据行变成列
对于mysql / hive 再进行统计的时候假设须要行变成列,能够使用函数 CASE 字段a WHEN 值b THEN c [WHEN d THEN e]* [ELSE f] END 当字段a=值 ...
- 2个YUV视频拼接技术
http://blog.csdn.net/huahuahailang/article/details/9040847 2个YUV视频拼接技术 http://zhongcong386.blog.163. ...
- opencv3.3.1 opencv_contribut 3.3.1 git 20180117最新版的在ubuntu1604上的编译
过程: 1. git clone ... contribut 2. git clone ... opencv 3. git checkout -b v3.3.1 4 gi ...
- ubantu 下 修改mysql 默认编码
启动mysql后,以root登录mysql root@Eadgar-virtual-machine:~# mysql -uroot -proot mysql> show variables li ...