ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / Process functions / Threads, semaphores, signals and mutex
  • Overview
  • What is a critical section
  • The CriticalSection variable
  • Critical section associated with a variable
  • Special case
  • Example
  • Remarks
  • Named critical section
  • How it works
  • Programming in WLanguage
  • Functions for managing critical sections
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.
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: A critical section is a semaphore limited to a single thread on a single code section (process, procedure, etc.).
What is a critical section
A critical section is a semaphore limited to a single thread on a single code section (process, procedure, etc.).
Unlike the semaphores, a critical section can be used once only.
For example, a critical section can be used to protect a procedure for updating the window controls.
Remark: The critical sections are unique for the application that created them. They cannot be shared between several applications.
WINDEV, WEBDEV and WINDEV Mobile offer several methods to implement critical sections.
The CriticalSection variable
The CriticalSection variable allows to easily implement a critical section thanks to the syntax USING .. IN.
Example:
s is CriticalSection
USING s IN
// code protected by the critical section
// only one thread will execute this code at a given moment
 
END
Remarks:
  • The CriticalSection type is used to simplify the declaration of critical sections.
  • CriticalSection variables respect the rules for variable scope.
AndroidAndroid Widget Not available in Android and in Android widget.
Critical section associated with a variable
When declaring a variable, this variable can be associated with a critical section by using the critical section attribute.
The syntax is as follows:
VariableName is VariableType <critical section>

or

VariableName is VariableType, critical section
The code sections that use these variables must be found between CriticalSectionStart and CriticalSectionEnd.

Special case

A critical section is automatically associated with the variables on which simple operations are performed, such as:
  • assigning a value.
  • retrieving a value.
  • incrementat, decrement (+, -, ++, --, +=, -= operators +, -, ++, --, += -=).

Example

// Global declarations of WIN_STAT window
gcySum is currency, critical section
gcyMax is currency, critical section
...
// Code run by several threads
cyOrderAmount is currency 
...
// atomic operation, the critical section is automatically managed by WLanguage
gcySum += cyAmountOrder

// multiple operation, it is necessary to implement the critical section explicitly
utiliser CriticalSection(gcyMax) IN
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
END

Remarks

  • The critical section attribute is allowed for:
    • the global project variables, set of procedures, window, page and report.
    • the local variables.
    • the members of classes.
    • the arrays: in this case, the attribute is associated with the array and not with the array elements.
  • The queues and the stacks are protected by default: the critical section attribute is not required.
AndroidAndroid Widget Not available in Android and in Android widget.
Named critical section

How it works

The critical section is defined by CriticalSectionStart and ended by CriticalSectionEnd.
The name of the CriticalSection is passed as a parameter to these functions via a string.
Example:
// Use of a named critical section
CriticalSectionStart("MySection")
// Two threads will not be able to run this code at the same time
...
CriticalSectionEnd("MySection")
This method of using critical sections has several drawbacks:
  • risk of collisions between several critical sections with the same name.
  • never-released critical sections.
AndroidAndroid Widget In Android, only the syntax with a named critical section is available.
Programming in WLanguage

Functions for managing critical sections

The following WLanguage functions are used to manage critical sections:
CriticalSectionEnables a name or on-variable critical section in the statement USING ... IN.
CriticalSectionEndMarks the end of critical section: the code will be run by another thread.
CriticalSectionStartMarks the start of a critical section: no other thread will be able to run the code as long as the current thread does not exit from the critical section.
To manage critical sections, it is also possible to use the following type:
CriticalSectionCriticalSection variables are used to define critical sections to limit the simultaneous execution of a code (procedure, line of code, etc.) to one thread at a given moment in an application.
AndroidAndroid Widget This type of variable is not available.
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