Jim Fell
Jim Fell

Reputation: 14256

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,
    ::GetProcessIdOfThread( hReceivingThreadHandle ),
    &hMyDupHandle,
    0,
    TRUE,
    DUPLICATE_SAME_ACCESS
    );

Unfortunately, I cannot be 100% certain about this bug because multi-thread debugging is tricky. Thanks.

Upvotes: 2

Views: 1136

Answers (2)

David Heffernan
David Heffernan

Reputation: 612804

You can share event handles between different threads in a process. Your bug lies elsewhere.

Upvotes: 1

Mark Wilkins
Mark Wilkins

Reputation: 41222

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.

Upvotes: 2

Related Questions