views:

267

answers:

3

Basically I need a replacement for Condition Variable and SleepConditionVariableCS because it only support Vista and UP. (For C++)

Some suggested to use Semaphore, I also found CreateEvent.

Basically, I need to have on thread waiting on WaitForSingleObject, until something one or more others thread tell me there is something to do.

In which context should I use a Semaphore vs an Win Event?

Thanks

A: 

In your case I'd use an event myself. Signal the event when you want the thread to get going. Job done :)

Edit: The difference between semaphores and events comes down to the internal count. If there are multiple ReleaseSemaphores then 2 WaitForSingleObjects will also be released. Events are boolean by nature. If 2 different places Signal event simultaneously then the wait will get released and it will get set back to unsignalled (dependent on if you have automatic or manual resetting). If you need it to be signalled from multiple places simultaneously and for the waiting thread to run twice then this event behaviour could lead to a deadlock.

Goz
+3  A: 

Replacing condition variables on Windows is extremely difficult and error-prone in the general case. Either:

  1. Use someone else's implementation (e.g., Boost.Thread).
  2. Rethink the problem you are trying to solve and see if Win32 can do it. Based on your description, an Event might suffice, but if the waiter needs to be triggered by some conditional expression that the other threads will setup, and not just a signal, you're better off going back to option 1.
Marcelo Cantos
+3  A: 

Use boost::condition_variable if at all possible. I've been down this road before (see msg on microsoft.public.win32.programmer.kernel) and the Win32 Event API does not suffice; there are problems using events.

Jason S