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 mutex?
  • Steps:
  • Remarks
  • Functions for managing a mutex
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
A mutex is 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.
Remark: Other systems can also be used to protect a code section:
  • 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.
  • 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.
Principle
The mutex was created by MutexCreate.
  1. Thread #1 runs MutexStart: there is no thread in the mutex.
  2. Thread #1 runs the code section protected by the mutex.
  3. While thread #1 runs the code protected by the mutex, thread #2 runs MutexStart: since the code protected by the mutex is being run by thread #1, thread #2 must wait for the mutex to be released.
  4. Thread #1 runs MutexEnd: no other thread runs the mutex code.
  5. Thread #2 can run the code protected by the mutex.
  6. Thread #2 runs MutexEnd: no other thread runs the mutex code.
How to implement a mutex?

Steps:

The different steps for implementing a mutex are as follows:
  1. Creating a mutex with MutexCreate. The mutex is associated with a name.
  2. Calling MutexStart before the code section to protect.
  3. Calling MutexEnd to define the code section to protect. The lines of code after MutexEnd will no longer be protected.
  4. Destroying the mutex with MutexDestroy.

Remarks

  • The code sections protected by a mutex must be as short as possible and they must only affect the "critical" processes.
  • When a thread is pending, the resources of the processor are not used.
  • A mutex can be applied 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.
  • MutexStart and MutexEnd must be used in the same process (in a procedure for example).
  • A mutex can be shared (or not) between the different applications run on the computer. When creating a mutex, specify how it will be shared (MutexCreate).

Functions for managing a mutex

The following WLanguage functions are used to manage mutexes:
MutexCreateExplicitly creates a mutex.
MutexDestroyExplicitly destroys a mutex.
MutexEndSignals that the thread frees the mutex.
MutexStartLocks the current thread while waiting for the mutex to be freed.
Minimum version required
  • Version 16
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 06/21/2023

Send a report | Local help