| Critical Section |
Article Index for Critical |
Website Links For Critical |
Information AboutCritical Section |
| CATEGORIES ABOUT CRITICAL SECTION | |
| concurrency control | |
| programming constructs | |
|
By carefully controlling which variables are modified inside and outside the critical section (usually, by accessing important state only from within), concurrent access to that state is prevented. APPLICATION LEVEL CRITICAL SECTIONS Application -level critical sections reside in the Memory range of the process and are usually modifiable by the process itself. This is called a User-space object because the program ran by the user (as opposed to the Kernel ) can modify and interact with the object. However the functions called may jump to Kernel-space code to register the user-space object with the kernel. Example Code For Critical Sections with Win32 API
#include
InitializeCriticalSection(&cs);
EnterCriticalSection(&cs);
LeaveCriticalSection(&cs);
DeleteCriticalSection(&cs); Note that on Windows NT (not 9x/ME), you can use the function TryEnterCriticalSection() to attempt to enter the critical section. This function returns immediately so that the thread can do other things if it fails to enter the critical section (usually due to the fact that another thread has locked it). Note that the use of a CriticalSection is not the same as a Win32 Mutex Object , which is used for ''inter-process'' synchronization. A Win32 CriticalSection is for ''inter-thread'' synchronization (and is much faster as far as lock times), however it cannot be shared across processes. KERNEL LEVEL CRITICAL SECTIONS Typically, critical sections prevent process and thread migration between processors and the Preemption of processes and threads by interrupts and other processes and threads. Critical sections often allow nesting. Nesting allows multiple critical sections to be entered and exited at little cost. If the Scheduler interrupts the current process or thread in a critical section, the scheduler will either allow the process or thread to run to completion of the critical section, or it will schedule the process or thread for another complete quantum. The scheduler will not migrate the process or thread to another processor, and it will not schedule another process or thread to run while the current process or thread is in a critical section. Similarly, if an Interrupt in a critical section, the interrupt's information is recorded for future processing, and execution is returned to the process or thread in the critical section. Once the critical section is exited, and in some cases the scheduled quantum completes, these pending interrupt will be executed. Since critical sections may Execute only on the processor on which they are entered, synchronization is only required within the executing processor. This allows critical sections to be entered and exited at almost zero cost. No interprocessor synchronization is required, only instruction stream synchronization. Most processors provide the required amount of synchronization by the simple act of interrupting the current execution state. This allows critical sections in most cases to be nothing more than a per processor count of critical sections entered. Performance enhancements include executing pending interrupts at the exit of all critical sections and allowing the scheduler to run at the exit of all critical sections. Further more, pending interrupts may be transferred to other processors for execution. Critical sections should not be used as a long lived locking primitive. They should be short enough that the critical section will be entered, executed, and exited without any interrupts occurring, from neither Hardware much less the scheduler. SEE ALSO |
|
|