INFO: Reducing the Count on a Semaphore Object

ID: Q94997


The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API), used with:
    • Microsoft Windows NT versions 3.1, 3.5, 3.51, 4.0
    • Microsoft Windows 95
    • Microsoft Windows 2000


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.

Additional query words: 3.10 3.50

Keywords : kbAPI kbKernBase kbNTOS310 kbNTOS350 kbNTOS351 kbNTOS400 kbWinOS2000 kbThread kbThreadSync kbWinOS95 kbDSupport kbGrpKernBase
Version : winnt:3.1,3.5,3.51,4.0
Platform : winnt
Issue type : kbinfo


Last Reviewed: January 11, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.