ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / Process functions / Threads, semaphores, signals and mutex
  • Overview
  • Principle
  • How to implement a semaphore?
  • Steps:
  • Remarks
  • Functions for managing semaphores
  • Example
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
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.
Principle
The semaphore was created by SemaphoreCreate.
  1. Thread #1 runs SemaphoreStart: there is no thread in the semaphore.
  2. Thread #1 runs the code section protected by the semaphore.
  3. 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.
  4. Thread #1 runs SemaphoreEnd: no thread runs the semaphore code.
  5. Thread #2 can run the code protected by the semaphore.
  6. Thread #2 runs SemaphoreEnd: no thread runs the semaphore code.
How to implement a semaphore?

Steps:

The different steps for implementing a semaphore are as follows:
  1. Creating a semaphore with SemaphoreCreate. The semaphore is associated with a name.
  2. Calling SemaphoreStart before the code section to protect.
  3. Calling SemaphoreEnd to define the code section to protect. The lines of code after SemaphoreEnd will no longer be protected.
  4. Destroying the semaphore with SemaphoreDestroy.

Remarks

  • 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).
Java In Java, semaphores are unique for the application that created them. They cannot be shared between several applications.

Functions for managing semaphores

The following WLanguage functions are used to manage semaphores:
SemaphoreCreateCreates a semaphore.
SemaphoreDestroyExplicitly destroys a semaphore.
SemaphoreEndAllows one or more threads to exit from the area protected by the semaphore.
SemaphoreStartLocks the current thread until the semaphore is opened (which means until a "free" spot becomes available in the protected section).

Example

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.
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 06/21/2023

Send a report | Local help