Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32 Event vs Semaphore

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

like image 539
JP. Avatar asked Jan 23 '23 02:01

JP.


1 Answers

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.

like image 90
Goz Avatar answered Jan 24 '23 14:01

Goz