Using Event Handles Across Threads - C++
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,
::GetP开发者_StackOverflow中文版rocessIdOfThread( hReceivingThreadHandle ),
&hMyDupHandle,
0,
TRUE,
DUPLICATE_SAME_ACCESS
);
Unfortunately, I cannot be 100% certain about this bug because multi-thread debugging is tricky. Thanks.
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.
精彩评论