views:

403

answers:

3

Is it safe to have 2 or more threads call the Win32 API's SetEvent on the same event handler not being protected by a critical section?

A: 

Assuming you have multiple threads waiting on the same event, running the same code.

  1. If your code doesnt clear the event until its done processing, you effectively have a CS. Since the event remains signaled until it is cleared(aka not autoreset), having multiple threads signal the does nothing except spin the CPU.
  2. If your code clears it at the begining of processing or the event is autorset, then you would have multiple threads running the same function, which is unsafe if these threads share anything.
Sanjaya R
A: 

there are no restrictions on calling SetEvent from multiple threads.

Andrey
+1  A: 

It's safe, but remember that if one thread Sets it, and another thread Sets it at the same time, you're not going to get two notifications, just one; since the 2nd one changed it from True to...True. If you're worried about this, use Semaphores instead.

Paul Betts