- What is a critical section
- The CriticalSection variable
- Critical section associated with a variable
- Special case
- Named critical section
- How it works
- Programming in WLanguage
- Functions for managing critical sections
Managing the critical sections
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
variable allows to easily implement a critical section thanks to the syntax USING .. IN
s is CriticalSection
USING s IN
// code protected by the critical section
// only one thread will execute this code at a given moment
- The CriticalSection type is used to simplify the declaration of critical sections.
- CriticalSection variables respect the rules for variable scope.
Critical section associated with a variable
When declaring a variable, this variable can be associated with a critical section by using the critical section
The syntax is as follows:
VariableName is VariableType <critical section>
VariableName is VariableType, critical section
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 +, -, ++, --, += -=).
// 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
USING CriticalSection(gcyMax) IN
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
- 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.
How it works
The name of the CriticalSection is passed as a parameter to these functions via a string.
// Use of a named critical section
// Two threads will not be able to run this code at the same time
This method of using critical sections has several drawbacks:
- risk of collisions between several critical sections with the same name.
- never-released critical sections.
Functions for managing critical sections
The following WLanguage functions are used to manage critical sections:
|CriticalSection||Enables a name or on-variable critical section in the statement USING ... IN.|
|CriticalSectionEnd||Marks the end of critical section: the code will be run by another thread.|
|CriticalSectionStart||Marks 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:
|CriticalSection||CriticalSection 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.|
This page is also available for…