Simulate a semaphore

[Autolink] Menu

You can use directory names, queue names (see also Using REXX queues to simulate a semaphore) or file names (see Using a file as semaphore) as semaphores in REXX (Note that the REXXUTIL DLL released with Object-Oriented REXX contains functions allowing use of normal OS/2 semaphores in REXX programs):

1. Using a directory name

 
/* example code to show how to use a directory name as semaphore in   */
/* REXX. Note that this algorithm assumes that the program also       */
/* frees the semaphore in case of a crash !                           */

  semName = "C:\TEMP\MYSEM"
                                /* test and set the semaphore         */
  do until rc = 0
                                /* note: in a real program you should */
                                /*       also check for a timeout!    */
    "@md " semName "1>NUL 2>NUL"
  end /* do until rc = 0 */

                                /* semaphore set -- now it's our turn */
                                /* to do something                    */
/* ... */

                                /* free the semaphore                 */
  "@rd " semName  "1>NUL 2>NUL"
exit 0

2. Using a Queue name

 
/* example code to show how to use a queue name as semaphore in       */
/* REXX. Note that this algorithm assumes that the program also       */
/* frees the semaphore in case of an crash!                           */

  semName = "MYSEM"
                                /* test and set the semaphore         */
  do until uniqueName == semName
                                /* note: in a real program you should */
                                /*       also check for a timeout!    */

    uniqueName = rxqueue( "create", semName )
    if uniqueName <> semName then
    do
                                /* queue already exists -- delete the */
                                /* new created queue!                 */
      call rxqueue "delete", uniqueName
    end /* if uniqueName <> semName then */
  end /* do until uniqueName == semName */

                                /* semaphore set -- now it's our turn */
                                /* to do something                    */
/* ... */

                                /* free the semaphore                 */
  call rxqueue  'Delete', semName
exit 0


[Back: Create a unique name]
[Next: Using REXX queues to simulate a semaphore]