Semaphores

Semaphores are used to synchronize access to limited resources. They can also be used to implement a notification event. Semaphores are provided by the TwinCAT real-time through the CSemaphoreInstance class.

CSemaphoreInstance

The CSemaphoreInstance class provides the interface for handling semaphores. To create a semaphore, the instance requires the object ID of the TwinCAT real-time instance, which is available via OID_TCRTIME_CTRL, as well as a reference to the TwinCAT object server via a pointer to ITComObjectServer.

Methods:

MAKE_RTOS_HRESULT(51)

Semaphore overflow. For example, if the internal memory is insufficient.

RTIME_NOWAIT (-1)

Method immediately returns S_OK if the semaphore is available.
Method returns a timeout if no resource is available.

RTIME_ENDLESSWAIT (0)

Method waits indefinitely for availability of the semaphores

The method returns S_OK if the semaphore was successfully obtained; otherwise:

MAKE_RTOS_HRESULT(10)

Indicates a timeout. If nTimeout is set as RTIME_NOWAIT, this semaphore is not available.

Sample:

Sample24: Semaphores