Reputation: 5585
I have two threads that communicate in the following way: Thread A posts a message in a message queue and Thread B processes that message. Thread A has to wait until Thread B processes the message.
Thread A
.........
post a message on the message queue
WaitForSingleObject (hEvent)
Use the message processed information
SetEvent(hEvent)
.........
Thread B
Process the message in the message queue
SetEvent(hEvent)
Do you see any problems with the above code? Do I need to call ResetEvent() anywhere? Is the SetEvent() call required in Thread A or Thread A should only call WaitForSingleObject() and Thread B should only call SetEvent()?
Thanks in advance
Upvotes: 0
Views: 1353
Reputation: 15870
A problem you will need to consider is the potential deadlock with this. If Thread A has to wait until Thread B processes the event before continuing (and your WaitForSingleObject call does not have a useful timeout value passed to it), there is a chance that Thread B will never process the event and will lock Thread A.
Upvotes: 2
Reputation: 133122
You needn't call ResetEvent
as long as the event is an AutoReset Event. This parameter is set in CreateEvent
. I think your pseudocode is OK.
Upvotes: 4