DosCloseSem

DosCloseSem

#define INCL_DOSSEMAPHORES

USHORT  rc = DosCloseSem(SemHandle);

HSEM             SemHandle;     /* Semaphore handle  */

USHORT           rc;            /* return code */

Example

This example creates a semaphore named timeout.sem, then closes it.

#define INCL_DOSSEMAPHORES

#define SEM_OWNERSHIP 0
#define SEM_NAME "\\SEM\\timeout.sem"

HSEM   SemHandle;
USHORT rc;

   if(!DosCreateSem(SEM_OWNERSHIP,    /* Indicate ownership */
                    &SemHandle,       /* Semaphore handle */
                    SEM_NAME))        /* Semaphore name string */
      rc = DosCloseSem(SemHandle);    /* Semaphore handle */

The following example illustrates the serialization of access to a shared resource between threads of the same process. The program creates a nonexclusive system semaphore named resource.sem, requests access to the semaphore, clears it, and finally closes the semaphore. For an illustration of notification of events, see the example given in DosOpenSem, DosSemSet, or DosSemWait.

#define INCL_DOSSEMAPHORES

#include <os2.h>

#define SEM_NAME "\\SEM\\resource.sem"  /* Semaphore name */
#define TIMEOUT 1500L                   /* Timeout (in milliseconds) */


main()
{
  HSEM SemHandle;
  USHORT rc;

  /* Note: the semaphore could have been created by another    */
  /*       thread.                                             */
  DosCreateSem(CSEM_PUBLIC,         /* Ownership - nonexclusive */
               &SemHandle,          /* Semaphore handle (returned) */
               SEM_NAME);           /* Semaphore name */
  if(!(rc = DosSemRequest(SemHandle,       /* Semaphore Handle */
                         TIMEOUT)))        /* Timeout Period   */
  {
    /* Semaphore obtained; resource may now be used. */
    /* Clear the semaphore after using resource.     */
    if(DosSemClear(SemHandle))
    {
      /* Semaphore exclusively owned by another process --  */
      /* cannot clear now.                                  */
    }
  }
  else
  {
    /* Semaphore not obtained: error processing (i.e. switch on rc) */
  }
  /* Semaphore no longer needed; close it */
  if(DosCloseSem(SemHandle))
  {
    /* Semaphore is still set -- cannot close now */
  }
}


[Back: DosCloseQueue]
[Next: DosConnectNmPipe]