RtReleaseSemaphore

RtReleaseSemaphore increases the count of the specified semaphore object by a specified amount.

Syntax

BOOL RtReleaseSemaphore(
    HANDLE hSemaphore,
    LONG lReleaseCount,
    LPLONG lpPreviousCount
);

Parameters

hSemaphore

The semaphore object. RtCreateSemaphore or RtOpenSemaphore returns this handle.

lReleaseCount

The amount by which the semaphore object's current count is to be increased. The value must be greater than zero. If the specified amount causes the semaphore's count to exceed the maximum count that was specified when the semaphore was created, the count is not changed and the function returns FALSE.

lpPreviousCount

A pointer to a 64-bit variable receives the previous count for the semaphore. This parameter can be NULL if the previous count is not required.

Return Value

TRUE if the function succeeds, FALSE if the function fails

To get extended error information, call GetLastError.

Remarks

The state of a semaphore object is signaled when its count is greater than zero and non-signaled when its count is equal to zero. The process that calls RtCreateSemaphore specifies the semaphore's initial count. Each time a waiting thread is released because of the semaphore's signaled state, the count of the semaphore is decreased by one.

Typically, an application uses a semaphore to limit the number of threads using a resource. Before a thread uses the resource, it specifies the semaphore handle in a call to one of the wait functions. When the wait function returns, it decreases the semaphore's count by one. When the thread has finished using the resource, it calls RtReleaseSemaphore to increase the semaphore's count by one.

Another use of RtReleaseSemaphore is during an application's initialization. The application can create a semaphore with an initial count of zero. This sets the semaphore's state to non-signaled and blocks all threads from accessing the protected resource. When the application finishes its initialization, it uses RtReleaseSemaphore to increase the count to its maximum value to permit normal access to the protected resource.

RtReleaseSemaphore with high release counts (e.g., greater than 10), and RtSetEvent with ManualReset TRUE and a high number of waiting threads (e.g., greater than 10) will experience slightly longer latencies, which scale with the number of threads made run-able in the call. For the best determinism, developers should avoid designs that make a large number of threads run-able at one time. If within a single process, you can call RtReleaseFastSemaphore.

Requirements

Minimum Supported Version RTX64 2013
Header Rtapi.h
Library RtApi.lib (Windows), Rtx_Rtss.lib (RTSS)

See Also:

RtCreateSemaphore

RtOpenSemaphore