PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • The critical section
  • Overview
  • Associating a critical section with a variable
  • Special case: Automatic critical section
  • 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.
  • 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.
The critical section

Overview

A critical section is a semaphore limited to a single thread on a single code section (process, procedure, ...).
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.
Example:
CriticalSectionStart()
// Two threads will not be able to run this code at the same time
...
CriticalSectionEnd()
Versions 21 and later
AndroidAndroid Widget Not available in Android and in Android Widget

Associating a critical section 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.
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
...
CriticalSectionStart(gcySum)
gcySum += cyAmountOrder
CriticalSectionEnd(gcySum)

CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
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
AndroidAndroid Widget Not available in Android and in Android Widget

Associating a critical section 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.
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
...
CriticalSectionStart(gcySum)
gcySum += cyAmountOrder
CriticalSectionEnd(gcySum)

CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
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.
AndroidAndroid Widget Not available in Android and in Android Widget

Associating a critical section 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.
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
...
CriticalSectionStart(gcySum)
gcySum += cyAmountOrder
CriticalSectionEnd(gcySum)

CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
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.
Versions 21 and later
AndroidAndroid Widget Not available in Android and in Android Widget

Special case: Automatic 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 +, -, ++, --, += -=).
In the following example, you can avoid implementing a critical section on the total value of the "gvySum" variable.
// Global declarations of WIN_STAT window
gcySum is currency, critical section
gcyMax is currency, critical section
...
// Code run by several threads
cyOrderAmount is currency

...
// Automatic critical section (simple operation)
gcySum += cyAmountOrder

// Non-automatic critical section (non-simple operation)
CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
New in version 21
AndroidAndroid Widget Not available in Android and in Android Widget

Special case: Automatic 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 +, -, ++, --, += -=).
In the following example, you can avoid implementing a critical section on the total value of the "gvySum" variable.
// Global declarations of WIN_STAT window
gcySum is currency, critical section
gcyMax is currency, critical section
...
// Code run by several threads
cyOrderAmount is currency

...
// Automatic critical section (simple operation)
gcySum += cyAmountOrder

// Non-automatic critical section (non-simple operation)
CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)
AndroidAndroid Widget Not available in Android and in Android Widget

Special case: Automatic 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 +, -, ++, --, += -=).
In the following example, you can avoid implementing a critical section on the total value of the "gvySum" variable.
// Global declarations of WIN_STAT window
gcySum is currency, critical section
gcyMax is currency, critical section
...
// Code run by several threads
cyOrderAmount is currency

...
// Automatic critical section (simple operation)
gcySum += cyAmountOrder

// Non-automatic critical section (non-simple operation)
CriticalSectionStart(gcyMax)
IF cyAmountOrder > gcyMax THEN
gcyMax = cyAmountOrder
END
CriticalSectionEnd(gcyMax)

The functions for managing the critical sections

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