AndroidDev
AndroidDev

Reputation: 5585

One way communication between two threads through Events

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

Upvotes: 0

Views: 1353

Answers (2)

Zac Howland
Zac Howland

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

Armen Tsirunyan
Armen Tsirunyan

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

Related Questions