来源: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的使用的更多相关文章

  1. C++多线程的几个重要方法解析CreateEvent / SetEvent /ResetEvent/ 等

    1.CreateEvent 是创建windows事件的意思,作用主要用在判断线程退出,程锁定方面. 函功能描述:创建或打开一个命名的或无名的事件对象. HANDLE m_hExit; m_hExit= ...

  2. 事件函数SetEvent、PulseEvent与WaitForSingleObject详解

    系统核心对象中的Event事件对象,在进程.线程间同步的时候是比较常用,发现它有两个出发函数,一个是SetEvent,还有一个PulseEvent, 两者的区别是: SetEvent为设置事件对象为有 ...

  3. 线程中CreateEvent和SetEvent及WaitForSingleObject的用法

    首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面. CreateEvent 函功能描述:创建或打开一个命名的或无名的事件对象. EVENT有两种状态 ...

  4. SetEvent/ResetEvent

    在自己主动重置事件对象中,当WaitSingleObject/WaitForMultipleObjects接收到SetEvent发送过来的信号后则返回WAIT_OBJECT_0,此时操作系统(待定)自 ...

  5. CreateEvent、SetEvent、ResetEvent和WaitForSingleObject

    事件对象就像一个开关:它仅仅有两种状态---开和关.当一个事件处于"开"状态.我们称其为"有信号".否则称为"无信号". 能够在一个线程的运 ...

  6. c++中SetEvent和ResetEvent的使用

    关于事件 事件(Event)是WIN32提供的最灵活的线程间同步方式,事件可以处于激发状态(signaled or true)或未激发状态(unsignal or false).根据状态变迁方式的不同 ...

  7. MyEvent.SetEvent; // 同步信号置位

    MyEvent.SetEvent;   //  同步信号置位 TSimpleEvent.Create = TEvent.Create(nil, True, False, nil)           ...

  8. CreateEvent和SetEvent及WaitForSingleObject的使用方法

    CreateEvent: 1.函数功能: 创建一个命名或匿名的事件对象 2.函数原型: HANDLE CreateEvent(   LPSECURITY_ATTRIBUTES lpEventAttri ...

  9. Windows CreateEvent,SetEvent,WaitForSingleObject的用法

    http://blog.pfan.cn/embed/19089.html WaitForSingleObject的用法 DWORD WaitForSingleObject(   HANDLE hHan ...

随机推荐

  1. mongodb模拟生产环境的分片集群

       分片是指数据拆分 将其分散在不同的机器上的过程,有时候也叫分区来表示这个概念.将数据分散到不同机器上 不需要功能强大的计算机就可以储存更多的数据,处理更大的负载.        几乎所有的数据库 ...

  2. HDU5294——Tricks Device(最短路 + 最大流)

    第一次做最大流的题目- 这题就是堆模板 #include <iostream> #include <algorithm> #include <cmath> #inc ...

  3. 网站添加ico图标

    打开某一个网页会在浏览器的标签栏处显示该网页的标题和图标,当网页被添加到收藏夹或者书签中时也会出现网页的图标,怎么在网页title左边显示网页的logo图标呢? 方法一(被动式): 制作一个ico格式 ...

  4. Node.js 抓取电影天堂新上电影节目单及ftp链接

    代码地址如下:http://www.demodashi.com/demo/12368.html 1 概述 本实例主要使用Node.js去抓取电影的节目单,方便大家使用下载. 2 node packag ...

  5. 为备考二级C语言做的代码练习---辅导资料《C语言经典编程282例》--(1)

    因为二级考试的时候用的C语言编译器是VC++6.0 真是日了狗了 用这个编译器 这是我第2个C编译器吧,第一个用的是啊哈C编译器..第二个是VS++6.0 然后在win下用VS2013感觉挺不错的 毕 ...

  6. 【Java项目实战】——DRP之HTML总结

    在DRP的学习之中,又将之前BS的内容又一次复习了一遍,借着复习的机会将BS的各个部分再又一次总结一下.今天来总结一下HTML. 在学习BS之后就进入了权限系统的开发之中,可是仍然发现非常多代码不会不 ...

  7. Redis闲谈(1):构建知识图谱

    场景:Redis面试 (图片来源于网络) 面试官: 我看到你的简历上说你熟练使用Redis,那么你讲一下Redis是干嘛用的? 小明: (心中窃喜,Redis不就是缓存吗?)Redis主要用作缓存,通 ...

  8. HDFS源码分析数据块复制之PendingReplicationBlocks

    PendingReplicationBlocks实现了所有正在复制的数据块的记账工作.它实现以下三个主要功能: 1.记录此时正在复制的块: 2.一种对复制请求进行跟踪的粗粒度计时器: 3.一个定期识别 ...

  9. python使用模板手记

    1.首先是$符号 在webpy中,模板html里面可以写python代码,但要用$开始.但如果网页代码本来就有$符号(javascript或者正则表达式),我们需要对其进行转意.用$$代替$ 给jqu ...

  10. 玩家下线(GS部分)

    玩家下线,之前一直感觉这个过程有点复杂 else if (stat == link_stat::link_disconnected || stat == link_stat::link_connect ...