- How to implement a semaphore?
- Functions for managing semaphores
Managing the semaphores in the threads: limiting the simultaneous code execution
Semaphores are used to limit the simultaneous execution of a code (procedure, line of code, etc.) to one or more threads at a given time. A semaphore can be shared between several applications.
For example: Two specific threads are used in a banking application:
- a thread to deposit funds into an account
- a thread to withdraw funds from an account.
At any given time, there can only be a single deposit or withdrawal.
Remark: Other systems can also be used to protect a code section:
- Mutexes are used to limit the simultaneous execution of a code (procedure, line of code, etc.) to one thread at a given time. A mutex can be shared between several applications. For more details, see Managing mutexes in threads.
- Critical sections are used to limit the simultaneous execution of a code (procedure, line of code, etc.) to one thread at a given time in a single application. For more details, see Managing the critical sections.
The semaphore was created by SemaphoreCreate
- Thread #1 runs SemaphoreStart: there is no thread in the semaphore.
- Thread #1 runs the code section protected by the semaphore.
- While thread #1 runs the code protected by the semaphore, thread #2 runs SemaphoreStart: since the code protected by the semaphore is being run by thread #1, thread #2 must wait for the semaphore to be released.
- Thread #1 runs SemaphoreEnd: no thread runs the semaphore code.
- Thread #2 can run the code protected by the semaphore.
- Thread #2 runs SemaphoreEnd: no thread runs the semaphore code.
How to implement a semaphore?
The different steps for implementing a semaphore are as follows:
- Creating a semaphore with SemaphoreCreate. The semaphore is associated with a name.
- Calling SemaphoreStart before the code section to protect.
- Calling SemaphoreEnd to define the code section to protect. The lines of code after SemaphoreEnd will no longer be protected.
- Destroying the semaphore with SemaphoreDestroy.
- The code sections protected by a semaphore must be as small as possible and they must only affect the "critical" processes.
- A semaphore with the same name can be used to protect several code sections. Only one thread may be in one of the areas protected by the semaphore at any given time.
- When a thread is pending, the resources of the processor are not used.
- The semaphores apply to the main thread and to the secondary threads (created by ThreadExecute). You must avoid locking the main thread. Indeed, if the main thread is locked (pending), the application cannot be run anymore.
- SemaphoreStart and SemaphoreEnd must be used in the same process (in a procedure for example).
- Semaphores can be shared (or not) between the different applications run on the computer. When creating the semaphores, specify how they will be shared (SemaphoreCreate).
Functions for managing semaphores
The following WLanguage functions are used to manage semaphores:
|SemaphoreCreate||Creates a semaphore.|
|SemaphoreDestroy||Explicitly destroys a semaphore.|
|SemaphoreEnd||Allows one or more threads to exit from the area protected by the semaphore.|
|SemaphoreStart||Locks the current thread until the semaphore is opened (which means until a "free" spot becomes available in the protected section).|
To perform an assignment shared by several threads, you must encapsulate in a semaphore the assignment of the variables as well as the read operation of these variables.
This page is also available for…