0
votes

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.

  1. Thread A

    .........
    post a message on the message queue
    WaitForSingleObject (hEvent)
    Use the message processed information
    SetEvent(hEvent)
    .........

  2. 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

2
"Thread A has to wait until Thread B processes the message" should make you rethink about necessity of using another thread.Bojan Komazec
Yes, I agree. But it is a bit of legacy code and the code comments state that all the processing of another COM server (that doesn't handle multi-threading) should be done in the same thread, so there is a separate thread just to process that.AndroidDev

2 Answers

4
votes

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.

2
votes

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.