Win32 Event vs Semaphore

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

Some suggested using Semaphore; I also found CreateEvent.

Basically, I need to have a WaitForSingleObject wait in a thread until something, one or more other threads tell me what to do.

In what context should I use Semaphore against a Win event?

thanks

+4
source share
3 answers

In your case, I myself would use the event. An event signal when you want the thread to start working. The task:)

Edit: the difference between semaphores and events comes down to an internal account. If there are multiple ReleaseSemaphores, then 2 WaitForSingleObjects will be released. Events are Boolean in nature. If a Signal event occurs in two different places at the same time, then the wait will be canceled and it will be returned to unsignalled (depending on whether you have an automatic or manual reset). If you need it to be signaled from several places at the same time, and to be executed twice for the waiting thread, this behavior of this event could lead to a deadlock.

+3
source

Replacing condition variables on Windows is extremely complex and error prone in the general case. Or:

  • Use a different implementation (e.g. Boost.Thread ).
  • Rethink the problem you are trying to solve and see if Win32 can do this. Based on your description, an event may be enough, but if the waiter needs to be called by some conditional expression that will be configured by other streams, and not just by a signal, you better return to option 1.
+3
source

Use boost :: condition_variable , if at all possible. I have already been on this path (see Msg on microsoft.public.win32.programmer.kernel ), and the Win32 API is not enough; there are problems using events.

+3
source

Source: https://habr.com/ru/post/1309420/


All Articles