跨线程使用事件句柄 - C++
我有一个应用程序,其中我跨线程共享事件句柄。这些事件句柄用于向应用程序发出串行 I/O 的传输完成和已接收数据通知。这些句柄作为类构造函数的传递参数或对 CreatThread
的调用复制到新线程。我认为这是有效的,但我遇到了一个奇怪的错误,似乎这些事件可能没有得到正确的信号。我应该为此使用 DuplicateHandle 函数吗?如果是这样,下面的用法正确吗?
::DuplicateHandle(
::GetCurrentProcessId(),
hMyHandle,
::GetProcessIdOfThread( hReceivingThreadHandle ),
&hMyDupHandle,
0,
TRUE,
DUPLICATE_SAME_ACCESS
);
不幸的是,我不能 100% 确定这个错误,因为多线程调试很棘手。谢谢。
I have an application wherein I am sharing event handles across threads. These event handles are used to signal transmit complete and received data notifications of serial I/O to the application. The handles are copied to the new threads as a passed parameter of class constructors or calls to CreatThread
. I thought this was working, but I've run into a weird bug where it seems like these events may not be getting properly signaled. Should I be using the DuplicateHandle
function for this? If so, would the following usage be correct?
::DuplicateHandle(
::GetCurrentProcessId(),
hMyHandle,
::GetProcessIdOfThread( hReceivingThreadHandle ),
&hMyDupHandle,
0,
TRUE,
DUPLICATE_SAME_ACCESS
);
Unfortunately, I cannot be 100% certain about this bug because multi-thread debugging is tricky. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果使用现有句柄的所有线程都在同一进程中,则不必使用该 API (DuplicateHandle)。同一进程中的线程可以对事件、信号量等使用相同的句柄值。
It should not be necessary to use that API (DuplicateHandle) if all the threads using the existing handle are in the same process. Threads within the same process can use the same handle value for events, semaphores, etc.
您可以在进程中的不同线程之间共享事件句柄。你的错误在别处。
You can share event handles between different threads in a process. Your bug lies elsewhere.