PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • 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/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
Semaphores are used to limit the simultaneous execution of a code (procedure, code line, 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:
  • Versions 16 and later
    Mutexes are used to limit the simultaneous execution of a code (procedure, code line, etc.) to one thread at a given time. A mutex can be shared between several applications. See Managing the mutexes in the threads for more details.
    New in version 16
    Mutexes are used to limit the simultaneous execution of a code (procedure, code line, etc.) to one thread at a given time. A mutex can be shared between several applications. See Managing the mutexes in the threads for more details.
    Mutexes are used to limit the simultaneous execution of a code (procedure, code line, etc.) to one thread at a given time. A mutex can be shared between several applications. See Managing the mutexes in the threads for more details.
  • Critical sections are used to limit the simultaneous execution of a code (procedure, code line, etc.) to one thread at a given time in a single application. See Managing the critical sections for more details.
Versions 18 and later
Universal Windows 10 App This feature is now available in Windows Store apps mode.
New in version 18
Universal Windows 10 App This feature is now available in Windows Store apps mode.
Universal Windows 10 App This feature is now available in Windows Store apps mode.
Versions 19 and later
WINDEVLinux This feature is now available for WINDEV applications in Linux.
Linux This feature is now available for WEBDEV sites in Linux.
New in version 19
WINDEVLinux This feature is now available for WINDEV applications in Linux.
Linux This feature is now available for WEBDEV sites in Linux.
WINDEVLinux This feature is now available for WINDEV applications in Linux.
Linux This feature is now available for WEBDEV sites in Linux.
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 code lines that are after SemaphoreEnd will not be protected anymore.
  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:
Versions 09 and later
SemaphoreCreate
New in version 09
SemaphoreCreate
SemaphoreCreate
Creates a semaphore.
Versions 09 and later
SemaphoreDestroy
New in version 09
SemaphoreDestroy
SemaphoreDestroy
Explicitly destroys a semaphore.
Versions 09 and later
SemaphoreEnd
New in version 09
SemaphoreEnd
SemaphoreEnd
Allows one or more threads to exit from the area protected by the semaphore.
Versions 09 and later
SemaphoreStart
New in version 09
SemaphoreStart
SemaphoreStart
Locks 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