- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在我的一个项目中,我创建了多个自动重置事件
和两个线程,线程使用WaitForMultipleObjects
在继续运行之前等待一些事件,例如:
HANDLE hTerminateEvent = CreateEvent(...); // auto reset
HANDLE hStateChangedEvent = CreateEvent(...); // auto reset
void thread1Func()
{
HANDLE handles[2] = { hTerminateEvent, hStateChangedEvent };
WaitForMultipleObjects(2, handles, FALSE/*bWaitAll*/, INFINITE);
...
}
void thread2Func()
{
HANDLE handles[2] = { hTerminateEvent, hStateChangedEvent };
WaitForMultipleObjects(2, handles, FALSE/*bWaitAll*/, INFINITE);
...
}
我以前认为一旦hTerminateEvent
被单选,两个线程都会被唤醒,但是对于自动重置事件似乎不是这样,哪个被唤醒是随机的,一个被唤醒后, 它将 hTerminateEvent
重置为未发出信号。
我的问题是如何解决这个问题:通过使用手动重置事件?或者是否存在任何设计来解决这个问题?谢谢!
最佳答案
如果您希望两个线程都对hTerminateEvent
使用react,则必须将其设置为手动重置。大概您将它用作告诉多个线程自行终止的信号,因此无论如何将其设置为自动重置是没有意义的。与 hStateChangedEvent
相同。
如果您阅读 CreateEvent()
文档,它说:
bManualReset [in]
If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the ResetEvent function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released.
...
When the state of a manual-reset event object is signaled, it remains signaled until it is explicitly reset to nonsignaled by the ResetEvent function. Any number of waiting threads, or threads that subsequently begin wait operations for the specified event object, can be released while the object's state is signaled.
When the state of an auto-reset event object is signaled, it remains signaled until a single waiting thread is released; the system then automatically resets the state to nonsignaled. If no threads are waiting, the event object's state remains signaled.
因此当使用自动重置事件时,您无法唤醒同时等待它的多个线程。
关于c++ - 在多个自动重置事件上使用 WaitForMultipleObjects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13831855/
如果我有以下 C++ 代码(适用于 Windows 操作系统): HANDLE hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL); HANDLE hMut
我有一个事件循环正在等待几个自动重置事件。使用 CreateEvent(NULL, false, false, NULL) 将事件全部初始化为数组 eventHandles_。 while (true
在我的一个项目中,我创建了多个自动重置事件和两个线程,线程使用WaitForMultipleObjects 在继续运行之前等待一些事件,例如: HANDLE hTerminateEvent = Cr
您知道 c 中的 Windows 库中的 WaitForMultipleObjects 函数正在获取作为对象数组的参数。但是在我的作业中,助手想要将所有线程保存在一个堆栈中而不是一个数组中,我如何将这
我正在尝试为我的 FileWatcher 类编写单元测试。 FileWatcher 派生自 Thread 类并使用 WaitForMultipleObjects 在其线程过程中等待两个句柄: 从Fin
我正在使用 WaitForMultipleObjects 函数来等待多个线程的完成,但我做错了,因为结果不是预期的 查看示例代码 type TForm1 = class(TForm) Me
在下面的测试程序中,每个测试线程在开始执行时将其句柄添加到全局 TThreadList 中,并在执行即将结束时从同一列表中删除其句柄。 出于测试目的,此外,每个线程确保在主线程锁定列表之前添加其句柄(
我的理解是WaitForMultipleObjects和 CRITICAL_SECTION旨在等待线程完成。并且它们都被描述为线程之间的进程和线程同步机制。如果它们旨在实现相同的目标,它们可以互换使用
假设:我用自动重置事件填充了一个句柄数组,并使用 bWaitAll = FALSE 将其传递给 WaitForMultipleObjects。 来自 MSDN:“当 bWaitAll 为 FALSE
我有一个事件对象 vector ,用于 WaitForMultipleObjects 函数。事件发出信号后,我尝试使用关闭句柄函数关闭事件,但我收到类似 Invalid handle was spec
我正在尝试制作一个测试控制台应用程序,它使用不同的线程从不同的 COM 端口打开和下载数据。下载数据后,我使用 WaitForMutipleObjects() 等待所有线程完成下载。 WaitForM
我已经阅读了之前发布的问题,该问题似乎与我在使用等待多个对象时遇到的错误相同,但我相信我的情况有所不同。我正在使用多个线程来计算 mandelbrot 集的不同部分。该程序编译并产生大约 5 次中的
如果我正在使用 WaitForMultipleObjects,并且该函数返回 WAIT_TIMEOUT,我如何才能获取导致超时发生的一个或多个对象? 我的另一个问题是,如果有多个对象发出信号,因为返回
我目前正在编写一个程序,该程序将同时运行多个程序组和其他程序。 if( WAIT_FAILED == WaitForMultipleObjects(numberOfProgramsRan, &info
我正在使用 WaitForMultipleObjects在 IPC 情况下,我有一个进程将数据写入两个内存映射文件中的一个或两个,另一个进程在数据更新时获取该数据。当任一 MMF 中的数据发生更改时,
在 Java (v6) 中实现 WaitForMultipleObjects 的 Win32 等价物的最优雅方法是什么。线程一直处于 hibernate 状态,直到发生多个事件之一。发生这种情况时,我
我不熟悉 WINAPI,我正在寻找一种方法来替换在一个示例中使用的 WaitForMultipleObjects,我只使用 Qt 将其移植到 Qt。有可能吗? 编辑:(根据评论要求提供更多信息) 第
我需要在多平台项目中使用 WaitForMultipleObjects 功能。 我可以用 Boost 中的什么来替代它? 最佳答案 我找到了这个问题的答案:不存在。 但是我已经找到并回答了 http:
根据微软的文档MAXIMUM_WAIT_OBJECTS是64(要等待的最大句柄数),但有时由于某种原因(因为随机)WaitForMultipleObjects返回 WAIT_FAILED ($FFFF
我正在尝试编写一个等待客户端连接的代码。一旦连接到客户端,它就应该开始读取文件并发送它。 我需要通知套接字句柄,也就是说,如果连接从客户端丢失,它会通知我,以便我可以尝试重新连接。 最佳答案 如果连接
我是一名优秀的程序员,十分优秀!