- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个应用程序,其中多个线程等待同一个事件对象发出信号。我看到的问题似乎是一种竞争条件,因为有时某些线程的等待状态 (WaitForMultipleObjects
) 由于事件信号而返回,而其他线程的等待状态显然看不到事件信号,因为他们没有返回。这些事件是使用 CreateEvent
作为手动重置事件对象创建的。
我的应用程序处理这些事件,以便在事件对象发出信号时,其“所有者”线程负责重置事件对象的信号状态,如以下代码片段所示。等待同一事件的其他线程不会尝试重置其信号状态。
switch ( dwObjectWaitState = ::WaitForMultipleObjects( i, pHandles, FALSE, INFINITE ) )
{
case WAIT_OBJECT_0 + BAS_MESSAGE_READY_EVT_ID:
::ResetEvent( pHandles[BAS_MESSAGE_READY_EVT_ID] );
/* handles the event */
break;
}
换句话说,我看到的问题似乎与 Remarks section for PulseEvent
on the MSDN website 中描述的内容有关。 :
If the call to PulseEvent occurs during the time when the thread has been removed from the wait state, the thread will not be released because PulseEvent releases only those threads that are waiting at the moment it is called. Therefore, PulseEvent is unreliable and should not be used by new applications. Instead, use condition variables.
如果发生这种情况,我能看到的唯一解决方案是让每个线程向该对象的所有者线程注册其对给定事件对象的使用,以便所有者线程可以确定何时可以安全地重置事件对象的信号状态。
有更好的方法吗?谢谢。
最佳答案
是的,有更好的方法:
[...] Instead, use condition variables.
http://msdn.microsoft.com/en-us/library/ms682052(v=vs.85).aspx
关于c++ - 等待事件的线程并不总是捕获事件信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5916894/
我是一名优秀的程序员,十分优秀!