INFO: Reducing the Count on a Semaphore Object (94997)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows NT 3.1
    • the operating system: Microsoft Windows NT 3.5
    • the operating system: Microsoft Windows NT 3.51
    • the operating system: Microsoft Windows NT 4.0
    • the operating system: Microsoft Windows 95
    • the operating system: Microsoft Windows 98
    • the operating system: Microsoft Windows Millennium Edition
    • the operating system: Microsoft Windows 2000
    • the operating system: Microsoft Windows XP

This article was previously published under Q94997

SUMMARY

ReleaseSemaphore(), which increments the count of a given semaphore object by a specified amount, will not take a negative value.

If, for some reason, you want to reduce the number of available semaphore "slots" to temporarily restrict or reduce access, you may loop calling WaitForSingleObject() with a zero timeout, counting the number of times it succeeds. When you no longer need to hold the semaphore slots, call ReleaseSemaphore() with the number of slots counted.

Note that this method does not prevent other threads from taking a semaphore slot when your thread is looping.

Modification Type:MinorLast Reviewed:9/27/2004
Keywords:kbAPI kbinfo kbKernBase kbThreadSync KB94997