CreateEvent的用法
事件对象就像一个开关:它只有两种状态---开和关。当一个事件处于”开”状态,我们称其为”有信号”否则称为”无信号”。可以在一个线程的执行函数中创建一个事件对象,然后观察它的状态,如果是”无信号”就让该线程睡眠,这样该线程占用的CPU时间就比较少。
产生事件对象的函数如下:
HANDLE CreateEvent(
LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
BOOL bManualReset, // reset type
BOOL bInitialState, // initial state
LPCTSTR lpName // object name
);
该函数创建一个Event同步对象,如果CreateEvent调用成功的话,会返回新生成的对象的句柄,否则返回NULL。
参数说明:
lpEventAttributes 一般为NULL
bManualReset 创建的Event是自动复位还是人工复位.如果true,人工复位, 一旦该Event被设置为有信号,则它一直会等到ResetEvent()API被调用时才会恢复 为无信号. 如果为false,Event被设置为有信号,则当有一个wait到它的Thread时, 该Event就会自动复位,变成无信号. 如果想 在每次调用WaitForSingleObject 后让WINDOWS为您自动地把事件地状态恢复为”无信号”状态,必须把该参数设为FALSE,否则,您必须每次调用ResetEvent函数来清除事件 的信号。
bInitialState 初始状态,true,有信号,false无信号
lpName 事件对象的名称。您在OpenEvent函数中可能使用。
注释:
一个Event被创建以后,可以用OpenEvent()API来获得它的Handle,用CloseHandle() 来关闭它,用SetEvent()或PulseEvent()来设置它使其有信号,用ResetEvent() 来使其无信号,用WaitForSingleObject()或WaitForMultipleObjects()来等待其变为有信号.
PulseEvent()是一个比较有意思的使用方法,正如这个API的名字,它使一个Event 对象的状态发生一次脉冲变化,从无信号变成有信号再变成无信号,而整个操作是原子的.
对自动复位的Event对象,它仅释放第一个等到该事件的thread(如果有),而对于人工复位的Event对象,它释放所有等待的thread.
这里有两个API函数用来修改事件对象的信号状态:SetEvent和ResetEvent。前者把事件对象设为”有信号”状态,而后者正好相反。
在事件对象生成后,必须调用WaitForSingleObject来让线程进入等待状态,该函数的语法如下:
WaitForSingleObject proto hObject:DWORD, dwTimeout:DWORD
hObject -->指向同步对象的指针。事件对象其实是同步对象的一种。
dwTimeout --> 等待同步对象变成”有信号”前等待的时间,以毫秒计。当等待的时间超过该值后无信号同步对象仍处于”无信号”状态,线程不再等待, WaitForSingleObject函数会返回。如果想要线程一直等待,请把该参数设为INFINITE(该值等于0xffffffff)。
发表于 @ 2008年04月18日 13:30:00|评论(0)|编辑
新一篇: PreCreateWindow的作用和使用方法 | 旧一篇: VC中_T()的作用
Visual C++中的多线程收藏
新一篇: 特洛伊木马(一) | 旧一篇: 用户接口与作业调度
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
这是一个典型的生产者-消费者问题,它们公用的资源是SharedBuffer,当Buffer中有数据且未满时,两个线程都可以运行,当Buffer为 空时,Consumer就要等待,直到Buffer不为空,这里就是用event来实现的;同样,当Buffer为满时,Producer就要等待。
发表于 @ 2006年10月29日 19:54:00|评论(2)|编辑
新一篇: 特洛伊木马(一) | 旧一篇: 用户接口与作业调度
Windows API一日一练(45)CreateEvent和SetEvent函数收藏
新一篇: Windows API一日一练(46)EnterCriticalSection和LeaveCriticalSection函数 | 旧一篇: Windows API一日一练(44)wsprintf函数
发表于 @ 2007年09月25日 21:32:00|评论(0)|编辑
新一篇: Windows API一日一练(46)EnterCriticalSection和LeaveCriticalSection函数 | 旧一篇: Windows API一日一练(44)wsprintf函数
Win32 API 常用函数之二收藏
新一篇: Win32 API 常用函数之三——注册表操作(上) | 旧一篇: Win32 API 常用函数之一
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
参数说明:
- 第一个参数同CreateThread类似,也是安全级别相关,通常被被设置为NULL,以获得默认的安全级别。
- 第二个参数是个布尔值,它能够告诉系统是创建一个人工重置的事件(TRUE)还是创建一个自动重置的事件( FALSE)。
- 第三个参数也是布尔值,用于指明该事件是要初始化为已通知状态(TRUE)还是未通知状态(FALSE)。
- 第四个参数是一个字符串,用于标示这个事件的名字。
以下是详细说明:
- 已通知状态和未通知状态
事件只有两种状态,已通知表示这个事件已经被设置过了(可以理解为发生了),未通知表示还没有发生。一般设置为未通知状态,并由SetEvent设置为已 通知状态。当然也可以反着做,CreateEvent时设置为已通知状态,然后由ResetEvent设置为未通知状态。 - 人工重置与自动重置
自动重置的事件定义了应该成功等待的副作用规则,即当线程成功地等待到该对象时,自动重置的事件就会自动重置到未通知状态。
人工重置则需要调用ResetEvent函数设置为未通知状态。 - 名字共享
这个参数很重要,Win32 API中有很多方法有这个参数,它遵从一种按名字共享的规则。
如果传入一个非NULL字符串(最多260个字符),那么在全局空间,共享该HANDLE,这个全局可以是跨进程的名字空间,即在另一个进程中依然能够使用该名字的HANDLE。
如果希望避免这种全局范围内的共享,那么应该传入NULL,以一种匿名的方式创建Event等,这样,它只在当前线程内可见。
当进程A创建了一个Event后,如CreateEvent(NULL,FALSE,FLASE,_T(“UniqueEvent”));进程B同样创建 了一个Event,也想起名字为UniqueEvent,那么就会出现问题:CreateEvent(NULL,FALSE,FALSE,_T (“UniqueEvent”));系统会首先查看是否已经存在了一个名字为“UniqueEvent”的对象,由于确实存在了一个带有改名字的内核对 象,因此内核要检查对象类型,同样是一 个Event,那么系统会执行一次安全检查,以确定调用者是否拥有对该对象的完整访问权。如果有这种访问权,系统会在进程B的句柄表里找到一个空项目,对 其初始化,使得该项指向现有的内核对象。如果类型不匹配,或者拒绝访问,那么进程B的CreateEvent会失败。
应用程序能够确定它是否确实创建了一个新内核对象,而不是打开了一个现有的对象。方法是在调用C r e a t e *函数后立即调用G e t L a s t E r r o r:如果为ERROR_ALREADY_EXISTS,那么表示系统内已经存在了这样名字的对象。
Open*是去查看名字空间中是否有这个名字的内核对象存在调用C r e a t e *函数与调用O p e n *函数之间的主要差别是,如果对象并不存在,那么C r e a t e *函数将创建该对象,而O p e n *函数则运行失败。
PulseEvent函数使得事件变为已通知状态,然后立即又变为未通知状态,这就像在调用SetEvent后又立即调用ResetEvent函数一样。 如果在人工重置的事件上调用PulseEvent函数,那么在发出该事件时,等待该事件的任何一个线程或所有线程将变为可调度线程。如果在自动重置事件上 调用P u l s e E v e n t函数,那么只有一个等待该事件的线程变为可调度线程。如果在发出事件时没有任何线程在等待该事件,那么将不起任何作用。
【等待函数】
等待函数用来监听事件的已通知状态。WaitForSingleObject和WaitForMultipleObjects两个函数分别用以等待单个事件和多个事件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
从函数原型上来看可知,事件的含义是能够支持被通知/未通知的内核对象(例如进程和线程,当传入的是进程或者线程句柄时,他表示等该线程或进程被标识为终止运行为止。)。
dwMilliseconds 参数表明等待的时间,如果在这个时间段中事件为已通知状态,那么对于Single版本将返回WAIT_OBJECT_0,对于Multiple版本将返回 WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1)。如果没有等到将返回WAIT_TIMEOUT。
Multiple版本中的bWaitAll表示想要让它使用何种方式等待。如果为该参数传递TRUE,那么在所有对象变为已通知状态之前,该函数将不允许调用线程运行。一般是FALSE,即只要有一个事件被相应,则线程可调度。
发表于 @ 2007年10月28日 18:04:00|评论(0)|编辑
CreateEvent的用法相关推荐
- html dom createevent,js 中 document.createEvent的用法
js 中 document.createEvent的用法 更新时间:2010年08月29日 23:22:02 作者: 用该方法创建了 Event 对象以后,必须用上表中所示的初始化方法初始化对象. ...
- js 中 document.createEvent的用法-转载
js 中 document.createEvent的用法 <a class="comment-mod" οnclick="alert('ss')" hre ...
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法
原文:线程中CreateEvent和SetEvent及WaitForSingleObject的用法
- 线程中CreateEvent和SetEvent和ReSetEvent及WaitForSingleObject的用法
概述 首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,程锁定方面. CreateEvent:创建或打开一个命名的或无名的事件对象. EVENT有两种状态:发信号 ...
- 转:线程中CreateEvent和SetEvent及WaitForSingleObject的用法
转自线程中CreateEvent和SetEvent及WaitForSingleObject的用法 首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,程锁定方面. ...
- 线程中CreateEvent、SetEvent、WaitForSingleObject的用法
原文地址:https://www.cnblogs.com/MrYuan/p/5238749.html 首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方 ...
- VC++之 CreateEvent和SetEvent及WaitForSingleObject的用法
CreateEvent: 1.函数功能: 创建一个命名或匿名的事件对象 2.函数原型: HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttri ...
- Windows CreateEvent,SetEvent,WaitForSingleObject的用法
http://blog.pfan.cn/embed/19089.html WaitForSingleObject的用法 DWORDWaitForSingleObject( HANDLE hHandle ...
- CreateEvent的使用方法
CreateEvent的使用方法收藏 新一篇: PreCreateWindow的作用和用法 | 旧一篇: VC中_T()的作用 事件对象就像一个开关:它仅仅有两种状态---开和关.当一个事件处于&qu ...
最新文章
- 2010年12月blog汇总:敏捷个人
- Java虚拟机 —— 垃圾回收机制
- 判断文件是否损坏_判断Excel文件是否已打开
- Kafka#4:存储设计 分布式设计 源码分析
- GDI绘图基本步骤总结(经典)
- linux pam模块 cron,Linux-PAM 1.1.2 中文文档 - 6.32. pam_tally-登录计数器(统计)模块 | Docs4dev...
- Java基本语法(10)--位运算符
- 书写css伪类时冒号前或后多个空格导致该规则失效-softbar
- 1360:奇怪的电梯(lift) 《信息学奥赛一本通》
- 斐波那契数列(Fibonacci)递归和非递归实现
- 数据结构(C++)—— 向量(Vector)
- java 调用 c# webservice 压缩 Liststring示例
- 中科微GPS模块使用教程 ATGM332D ATGM336H GSP模块 单片机程序 C程序
- 这年头能真正掌握Javaassist技术的程序员真不多
- 小米路由器 ping 测试软件,小米路由器3 测试: 自建简单智能家居
- mysql数据库数据恢复
- C++控制输出的字体颜色
- A. New Year and Hurry
- 微信公众号如何有效涨粉?学会这几种技巧让你流量倍增
- 查找书籍(20 分)