---
title: "Semaphore (_Semaphore class)"
canonical: "https://docs.cuesystem.com/space/CVCHELP/512983144/Semaphore%20(_Semaphore%20class)"
format: markdown
---
> Macro (toc)

---

# Overview 

The units possess a multi-task control system, which makes it possible to run several processes at once. In programming, it is however necessary to count with states of collision that may for example occur when more processes work with the same ports or share the same data. 

#### Lock and Unlock functions of class _Semaphore 

For example, imagine you are working with a PRESET.DAT file, in which you are loading data related to the settings of rooms, saving it via the SAVE_PRESET process, and reading it via the RECALL_PRESET process. If each of the processes starts running at a different moment, everything is all right. However, if one of the processes starts at a time when the other is running, unexpected troubles may occur. For example, the RECALL_PRESET process may initiate a situation when part of the equipment is put into a state based on the old data stored in the PRESET.DAT file (before the saving of new data), while the other part is set according to the new data (after saving). This situation is illustrated in the left part of Fig. 1. 

![image](media://27535b36-e8fd-44a1-a705-6e01d87abdd6)

The program must therefore be written in a way to prevent such collisions. If process1 calls the Lock command and then process2 calls the same Semaphore's Lock command too, process2 will wait until process1 calls the Unlock command or until process1 finishes. There is no influence on other processes running at the moment. 

From the systemic point of view, those commands have the following functions: 

When the Lock is executed in a process, the system checks whether some other process has already entered the same Semaphore's Lock. If not – which means the Semaphore is "unlocked" – the Semaphore status changes to Lock and the program continues carrying out the next commands in the given process. However, if the Semaphore is already "locked", the processing of commands in the given process stops until the Semaphore is unlocked again (usually by the process which has locked). The unlocking is done through the Unlock function, or it occurs when the process that has locked the Semaphore finishes. An example of using Lock and Unlock is shown in the right part of fig. 1. Using Lock and Unlock does not affect the running of other processes which do not use those commands. 

#### Multi-level lock 

This may be compared to the common multi-turn door lock. If you use more Lock commands one after another, every new Lock will lock the Semaphore to the next level (the second Lock command will lock the Semaphore to the second level, the third Lock command will lock Semaphore to the third level, etc.). If you use the Unlock, it will unlock the Semaphore to the previous level (until the whole Semaphore is unlocked) - see Fig. 2. In this case, the commands are executed in the locked state, as expected. 

![image](media://2a87f143-baf6-4ed5-adaa-4eb560ee3d69)

#### Every lock must be unlocked 

When using the Lock and Unlock, one has to remember to prevent locking the Semaphore without the possibility of unlocking it. If you use Lock, do not forget to put the Unlock in the program, otherwise, the Semaphore will remain locked until the process which has locked the Semaphore finishes. To prevent the slowing down of other processes waiting for unlocking do not use Lock for longer periods than necessary. 

Using the Lock and Unlock can also help you solve many conflict situations without having to program various kinds of flags. 

# Lock

The function locks a process. Only one process can be locked by the same Semaphore. No other process can be locked by the same Semaphore at the same time.  

The Semaphore allows one process at a time to access resources. It is useful when only one process at a time can be allowed to modify data or some other controlled resources. 

If process1 calls Lock and process2 will call the same Semaphore's Lock too, process2 will wait until the process1 calls Unlock or until the process1 finishes. 

#### Syntax 

```visualbasic
Function Lock() As Boolean
```

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs.

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[28](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#28)</u>.  

# Unlock

The function command causes an unlocking of Semaphore. If the function Unlock is not used, it is executed automatically at the end of the process. 

#### Syntax 

```visualbasic
Function Unlock() As Boolean
```

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. 

The function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[29](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#29)</u>.