PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • Principle
  • How to implement a semaphore?
  • The steps:
  • Notes
  • The 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
The semaphores are used to limit the simultaneous execution of a code (procedure, code line, ...) 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 credit the accounts
  • a thread to debit the accounts.
At any given time, there can only be a single account credit or a single account debit.
Note: Other systems can also be used to protect a code section:
  • Versions 16 and later
    The mutexes are used to limit the simultaneous execution of a code (procedure, code line, ...) 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
    The mutexes are used to limit the simultaneous execution of a code (procedure, code line, ...) 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.
    The mutexes are used to limit the simultaneous execution of a code (procedure, code line, ...) 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.
  • The critical sections are used to limit the simultaneous execution of a code (procedure, code line, ...) 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.
Android Widget This feature is now available in Android Widget mode.
New in version 18
Universal Windows 10 App This feature is now available in Windows Store apps mode.
Android Widget This feature is now available in Android Widget mode.
Universal Windows 10 App This feature is now available in Windows Store apps mode.
Android Widget This feature is now available in Android Widget mode.
Versions 19 and later
iPhone/iPad This feature is now available for the iPhone/iPad applications.
WINDEVLinux This feature is now available for the WINDEV applications in Linux.
WEBDEV - Server codeLinux This feature is now available for the WEBDEV sites in Linux.
New in version 19
iPhone/iPad This feature is now available for the iPhone/iPad applications.
WINDEVLinux This feature is now available for the WINDEV applications in Linux.
WEBDEV - Server codeLinux This feature is now available for the WEBDEV sites in Linux.
iPhone/iPad This feature is now available for the iPhone/iPad applications.
WINDEVLinux This feature is now available for the WINDEV applications in Linux.
WEBDEV - Server codeLinux This feature is now available for the WEBDEV sites in Linux.
Principle
The semaphore was created by SemaphoreCreate.
  1. The thread #1 runs SemaphoreStart: no thread is currently found in the semaphore.
  2. The thread #1 runs the code section protected by the semaphore.
  3. While the thread #1 runs the code protected by the semaphore, a thread #2 runs SemaphoreStart: the code protected by the semaphore being currently run by the thread #1, the thread #2 must wait for the semaphore to be freed.
  4. The thread #1 runs SemaphoreEnd: no thread runs the code of the semaphore.
  5. The thread #2 can run the code protected by the semaphore.
  6. The thread #2 runs SemaphoreEnd: no thread runs the code of the semaphore.
How to implement a semaphore?

The 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 found after SemaphoreEnd will not be protected anymore.
  4. Destroying the semaphore with SemaphoreDestroy.

Notes

  • 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. A single thread can be found at a given time in one of the areas protected by the semaphore.
  • 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).
  • The semaphores can be shared (or not) among the different applications run on the computer. All you have to do is specify the share mode of semaphores during their creation (SemaphoreCreate).
Java In Java, the semaphores are unique for the application that created them. They cannot be shared between several applications.

The functions for managing semaphores

The following WLanguage functions are used to manage the 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 required version
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment