Releasing a Mutex Semaphore

A thread can release ownership of a mutex semaphore by calling DosReleaseMutexSem. Each call to DosReleaseMutexSem decrements the request count that is maintained for the semaphore by OS/2. Each call to DosRequestMutexSem increments the count.

The following code fragment relinquishes ownership of a mutex semaphore. Assume that the handle of the semaphore has been placed into hmtx already.

    #define INCL_DOSSEMAPHORES   /* Semaphore values */
    #include <os2.h>
    #include <stdio.h>

    HMTX    hmtx;    /* Mutex semaphore handle */
    APIRET  ulrc;    /* Return code            */

    ulrc = DosReleaseMutexSem(hmtx);

    if (ulrc != 0) {
        printf("DosReleaseMutexSem error: return code = %ld",
               ulrc);
        return;
    }

Calls to DosRequestMutexSem and DosReleaseMutexSem can be nested, but the request count cannot exceed 65535. If an attempt is made to exceed this number, ERROR_TOO_MANY_SEM_REQUESTS is returned. When calls to DosRequestMutexSem and DosReleaseMutexSem are nested, a call to DosReleaseMutexSem merely decrements the request count for the semaphore; the semaphore is not actually released to another thread until its request count is 0.


[Back: Requesting a Mutex Semaphore]
[Next: Closing a Mutex Semaphore]