|
|
|
|
|
- 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
Managing the critical sections
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
utiliser s IN
END
Remarks: - 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 attribute. The syntax is as follows: VariableName is VariableType <critical section>
or
VariableName is VariableType, critical section 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
gcySum is currency, critical section
gcyMax is currency, critical section
...
cyOrderAmount is currency
...
gcySum += cyAmountOrder
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.
How it works The name of the CriticalSection is passed as a parameter to these functions via a string. Example:
CriticalSectionStart("MySection")
...
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.
Functions for managing critical sections The following WLanguage functions are used to manage critical sections: | | CriticalSection | Activates a named critical section or variable in a USE ... IN instruction. | CriticalSectionEnd | Marks the end of a critical section: another thread will be able to run the code. | CriticalSectionStart | Marks the beginning 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…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|