PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • Overview
  • What is a critical section
  • The CriticalSection variable
  • Critical section associated with a variable
  • Special case
  • Example
  • Notes
  • Named critical section
  • The principle
  • Programming in WLanguage
  • The functions for managing the critical sections
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.
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.
  • 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.
Note: 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.
Versions 24 and later
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
Notes:
  • The CriticalSection type is used to simplify the declaration of critical sections.
  • CriticalSection variables respect the rules for variable scope.
New in version 24
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
Notes:
  • The CriticalSection type is used to simplify the declaration of critical sections.
  • CriticalSection variables respect the rules for variable scope.
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
Notes:
  • The CriticalSection type is used to simplify the declaration of critical sections.
  • CriticalSection variables respect the rules for variable scope.
Versions 21 and later
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
CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
// 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
END
END

Notes

  • 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 in native mode: the critical section attribute is not required.
New in version 21
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
CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
// 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
END
END

Notes

  • 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 in native mode: the critical section attribute is not required.
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
CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
// 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
END
END

Notes

  • 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 in native mode: the critical section attribute is not required.
Named critical section

The principle

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.
Programming in WLanguage

The functions for managing the critical sections

The following WLanguage functions are used to manage the critical sections:
Versions 23 and later
CriticalSection
New in version 23
CriticalSection
CriticalSection
Enables a name or on-variable critical section in the statement USING ... IN.
Versions 09 and later
CriticalSectionEnd
New in version 09
CriticalSectionEnd
CriticalSectionEnd
Marks the end of critical section: the code will be run by another thread.
Versions 09 and later
CriticalSectionStart
New in version 09
CriticalSectionStart
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.
Versions 24 and later
To manage critical sections, it is also possible to use the following type:
Versions 24 and later
CriticalSection
New in version 24
CriticalSection
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.
New in version 24
To manage critical sections, it is also possible to use the following type:
Versions 24 and later
CriticalSection
New in version 24
CriticalSection
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.
To manage critical sections, it is also possible to use the following type:
Versions 24 and later
CriticalSection
New in version 24
CriticalSection
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.
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment