« Back to Product

Documentation

IPS_SemaphoreEnter

 bool IPS_SemaphoreEnter (string $Name, int $WaitingTime) 

Parameters

Name

Name that describes the semaphore

WaitingTime

Milliseconds to wait until the function gives up

Returns

TRUE if the semaphore was entered. FALSE if the semaphore could not be entered before the waiting time expired.

Description

This function sets a Semaphore (a sign with signal effect), which can be used to execute a part of the script exclusively and to protect it against competing scripts.

In IP-Symcon, multiple scripts can generally run simultaneously. As long as a script does not require exclusive access to variables or system resources, this presents no problem. If exclusive access is imperative, a semaphore can be set with this function. The function first checks if the semaphore with the specified name is already set. If it does not exist yet, it is set by the function and TRUE is returned. All other scripts that subsequently try to set the same semaphore keep trying to set it for the duration of WaitingTime. If this is successful, TRUE is returned, otherwise, after the waiting time has expired, FALSE.
If you want to ensure that the data used by a script is not altered by other, concurrently running scripts during its execution, you can use this function in order to secure exclusive access.

When using this function, it is vital to reset the semaphore via IPS_SemaphoreLeave after the critical part has been processed.

Example

if (IPS_SemaphoreEnter("CriticalPoint", 1000)) 
{
    // ...Critical code section

    //Release semaphore again!
    IPS_SemaphoreLeave("CriticalPoint");
}
else
{
    // ...No execution possible. Another script uses the "CriticalPoint" 
    // for more than 1 second, so our wait time is exceeded.
}
Any questions?