SEMAPHORE Data Type

Description

Use the SEMAPHORE data type to create semaphore variables, which are used in concurrent scripts to mutually exclude competing threads or control access to a resource.

Values of type SEMAPHORE can only be assigned a value once. The value must be an INTEGER greater than zero. Once it is set, your code can get the semaphore's value, but cannot set it.

When you allocate a semaphore variable, you must initialize it before you can invoke Acquire. For example, the following statement creates a binary semaphore that allows you to control access to a single resource.

SEMAPHORE semAlpha = 1

Use a semaphore to control the access of competing threads to a resource as follows:

  1. Before using a semaphore-protected resource of any description, invoke Acquire (semA), where semA is the semaphore assigned to this resource.

  2. When you have completed use of the resource protected by semA, invoke Release (semA) to make the semaphore, and therefore the resource, available to another thread.

Note: If your script experiences an exception in the code between acquisition of a semaphore and its release, the script will deadlock unless you use a do-except statement to handle the exception. Place the do statement after the Acquire and before any code that could cause an exception. Place a Release statement in the except block.