CreateEvent(NULL,FALSE,TRUE,NULL);后马上调用WaitForSingleObject,其作用是什么?

apao 2003-07-02 11:10:07
有一段程序如下:

……
HANDLE hHandle = CreateEvent(NULL, FALSE, TRUE, NULL);
if(hHandle != NULL)
{
DWORD dwWaitResult = WaitForSingleObject(hHandle, 0);
switch(dwWaitResult)
{
case WAIT_OBJECT_0:
……
break;
case WAIT_FAILED:
……
break;
}
}
……

这段程序的用意是什么?为什么创建了一个无名自动重置事件(初始状态是有信号)后,马上调用WaitForSingleObject?听说这是一种特殊用法(好像是为了强行CPU切换什么的),有谁清楚吗?
...全文
282 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongfa 2003-07-25
  • 打赏
  • 举报
回复
MSDN上说有很清楚了.

DWORD WaitForSingleObject(
HANDLE hHandle, // handle to object
DWORD dwMilliseconds // time-out interval
);

dwMilliseconds
[in] Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.

就是这句话了.
If dwMilliseconds is zero, the function tests the object's state and returns immediately.
opentuxedo 2003-07-04
  • 打赏
  • 举报
回复
等待时间为0时是对HANDLE进行检测。
WAIT_OBJECT_0说明HANDLE已经准备好。
如果是WAIT_TIMEOUT则说明这个HANDLE没有准备好,是一个不完全正确的HANDLE
apao 2003-07-04
  • 打赏
  • 举报
回复
opentuxedo(开缝的燕尾服) :
能说的详细点吗?谢谢!
opentuxedo 2003-07-03
  • 打赏
  • 举报
回复
TIME=0用于对内核对象进行检测。
现在在客户这里回去详细讲
penter 2003-07-02
  • 打赏
  • 举报
回复
自己把自己阻塞,让系统去调度其他的线程;
yh824 2003-07-02
  • 打赏
  • 举报
回复
创建了一个自动重置方式的事件,并且事件初始为有信号状态。
当用WaitForSingleObject(hHandle,0)捕捉到事件后,事件马上被重置为无信号状态
光看上边贴出的代码确实类似一个Sleep(0) ;


ArthurTu 2003-07-02
  • 打赏
  • 举报
回复
估计是这样的。
JoeRen 2003-07-02
  • 打赏
  • 举报
回复
当等待时间为0的时候,WaitForSingleObject和Sleep将当前线程的剩余时间片全部放弃。这时操作系统一般会切换到其他就绪线程执行。
作为对比,SwitchToThread函数也有类似的切换功能,但它不会放弃任何时间片。

以上代码相当于一句简单的Sleep(0),所以作用不大。当然,还创建了一个event供以后使用,呵呵
apao 2003-07-02
  • 打赏
  • 举报
回复
就是啊!所以说这种用法特殊嘛,不知道用意何在?
penter 2003-07-02
  • 打赏
  • 举报
回复
哈,一个EVENT,句柄只有线程自己知道,在创建的时候又是 signaled ,自己拥有它,还要测试一下,其他的线程没有 EVENT 的名字,也没有它的句柄,问题好象很复杂?!
penter 2003-07-02
  • 打赏
  • 举报
回复
HANDLE CreateEvent(
LPSECURITY_ATTRIBUTES lpEventAttributes,
BOOL bManualReset,
BOOL bInitialState,
LPCTSTR lpName
);

bInitialState
[in] If this parameter is TRUE, the initial state of the event object is signaled; otherwise, it is nonsignaled.

The WaitForSingleObject function checks the current state of the specified object. If the object's state is nonsignaled, the calling thread enters the wait state. It uses no processor time while waiting for the object state to become signaled or the time-out interval to elapse.

参数指定为零,只是测试一下状态,立即返回......

WAIT_ABANDONED The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.

问题在最后的英文里面,最上面的说法是错的,等高手吧;


opentuxedo 2003-07-02
  • 打赏
  • 举报
回复
先建立一个Event,然后用WaitForSingleObject等待Event触发.
可以在另一个县城中触发Event,以唤醒你现在的县城
penter 2003-07-02
  • 打赏
  • 举报
回复
等等,我再看看;
penter 2003-07-02
  • 打赏
  • 举报
回复
等等,我再看看;

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧