When a process no longer requires access to an event semaphore, it closes the semaphore by calling DosCloseEventSem.
The following code fragment closes an event semaphore. Assume that the handle of the semaphore has been placed into HEV already.
#define INCL_DOSSEMAPHORES /* Semaphore values */ #include <os2.h> #include <stdio.h> HEV hev; /* Event semaphore handle */ APIRET ulrc; /* Return code */ ulrc = DosCloseEventSem(hev); if (ulrc != 0) { printf("DosCloseEventSem error: return code = %ld", ulrc); return; }
Calls to DosOpenEventSem and DosCloseEventSem can be nested, but the usage count for a semaphore cannot exceed 65535. If an attempt is made to exceed this number, ERROR_TOO_MANY_OPENS is returned.
If a process ends without closing an open semaphore, the semaphore is closed by OS/2.
Each call to DosCloseEventSem decrements the usage count of the semaphore. This count is initialized to 1 when the semaphore is created and is incremented by each call to DosOpenEventSem. When the usage count reaches 0, the semaphore is deleted from OS/2. The call to DosCloseEventSem that decrements the usage count to 0 and causes the semaphore to be deleted is referred to as the final close. If a thread attempts to perform the final close for a semaphore while another thread in the same process is still waiting for it, ERROR_SEM_BUSY is returned.