Recently, some readers have encountered a known win32 semaphore counting error code. There are a number of factors that can cause this problem. Let’s take a look at them now.



There is no such type of “control account” associated with a Win32 semaphore, which is believed to be the reason why you cannot get it.

I am apparently saying that the number of semaphores will probably make some difference at some point, but from the perspective of a person in the chain, unless steps are taken to increase or decrease the number of semaphores. a thread can completely invalidate any response returned when it is finally calculated.

For this reason, the Windows API sync functions do not offer the ability to accept the previous number of grids without any side effects. The hand effect ensures that you have a valid window of time to display the value in a meaningful way.

  LONG account = 0;if (WAIT_OBJECT_0 == WaitForSingleObject (hSemaphore, 0L))  // the number of semaphores is at least one. ReleaseSemaphore (hSemaphore, 1, & count); 

win32 get semaphore count

Why is it better? Of course not. Possibly, but there is a program that does something useful between hold and release that would cause a race condition if ReleaseSemaphore were allowed to release 0.

A semaphore object is a new synchronization object that maintains a count from zero to the specified maximum value. The counter is each reduced free time during which the thread timed out due to the semaphore object, and is incremented individually when the thread frees the semaphores. When the counter reaches zero, no threads can successfully wait until the state of the semaphore object is indicated. The semaphore state is set to signal if this counter reading is greater than zero, is not signaled, and also if its counter reading becomes zero.

A semaphore object that is useful is to drop a share that a limited number of users can handle. For example, an application might have a limit on the number of windows it can create. It uses a semaphore with an identical maximum number at the border of the window, decrements the specific number when the window is likely to be created, and increases it when the window is closed. Attachedit does not become a semaphore object if one of the wait functions is successfully called before each window is created. The second count is zero – this indicates that the house window limit has been reached – the timeout for the job blocks the execution of the window creation code.


Careful thread uses the CreateSemaphore CreateSemaphoreEx plus function to create a semaphore product. The creation thread reports the tentative number and maximum number of accounts for the object. The initial value of the counter must not be less than zero or greater than most of the maximum value. The creation thread will also provide the name a for the person’s semaphore object. Other processing threads can open a handle to activate a semaphore object by specifying the company name in a call to a specific OpenSemaphore function. For more information on the names of the Mutex, Event, Semaphore, and Timer objects, see Inter-Process Synchronization .

If the semaphore is waiting on more than one thread, it is likely that a waiting thread has been selected. Don’t get ready for first-in-first-out (FIFO) principles. External events, i.e.Something like kernel mode APCs can change the order of the queue.

Each time one of the wait functions returns due to a semaphore expression signal, the number of semaphores is decreased by one. The ReleaseSemaphore function increments the semaphore number by a specified amount. The number can never be less than zero or greater than the maximum value.

win32 get semaphore count

The actual semaphore number is usually set to the maximum value. The counter is then decremented to the appropriate level because the protected resource is very heavily used. Alternatively, you can create a single semaphore with a zero seed to block access to the application’s security techniques while the resource is considered initialized. After initialization, you will probably use ReleaseSemaphore to increment to calculate the maximum value.

The thread that owns the mutex object can simply wait continuously until the destination of the mutex is reported, without blocking its implementation. However, a thread that constantly sticks to the same thing withthe emaphore product, will decrease the number of semaphores each time the wait actually ends; the wire is blocked when everything counts to zero. Likewise, a thread has full ownership that a single mutex can successfully invoke a ReleaseMutex job, even though any thread can use ReleaseSemaphore to increase the volume. The number of the ideal semaphore object.

A thread can reduce the number of actual semaphores more than once by repeating essentially the same idea of ​​a semaphore in calls to any of the exact wait functions by specifying a large array containing multiple descriptors for the same semaphore … does not require multiple decrements.

When you’re done using the semaphore object, call this special CloseHandle function to close the hosting. The semaphore object is destroyed when you close its last handle. Closing the handle does not change the number of semaphores; Therefore, you must confidently call ReleaseSemaphore before closing the handle or before terminating the process. Otherwise, pending operations will often expire Timeout or continue indefinitely, depending on whether a timeout value was specified.

Use a semaphore