From Appmethod Libraries
Jump to: navigation, search


Object Pascal

TSemaphore = class(THandleObject)


class PASCALIMPLEMENTATION TSemaphore : public THandleObject


Type Visibility Source Unit Parent
class public
System.SyncObjs System.SyncObjs


TSemaphore is a synchronization primitive that manages the concurrent execution of threads, in the context of sharing a common resource.

Use TSemaphore in multi-threaded applications that require safe access to a shared resource.

To each semaphore there is an associated counter variable. This variable gives the number of units of the shared resource that are currently free. The counter is decremented whenever a thread locks the semaphore and incremented when a thread releases the semaphore.

If a thread tries to lock the semaphore and the counter value is nil, then the thread adds to the semaphore waiting queue. The first thread in the semaphore queue locks the semaphore as soon as the counter value is nonzero.

When instantiating the TSemaphore class, the number of initial free units of the shared resource can be specified. This is actually the initial value of the semaphore counter variable.

Also, the number of available units of the shared resource can be specified through the AMaximumCount parameter of the TSemaphore class constructor.

Note: If the number of available units of the shared resource is one, then TSemaphore is called a binary semaphore and is basically equivalent to a TMutex.

See Also