Threre is a "guarded region" concept in windows, that is similar to critical region. Who knows how is it differs from Critical?
-
Do you mean "Critical Section"? (Never heard of "crytical region", "crytical" isn't an English word.) – Richard Jul 14 '14 at 10:34
-
yes Critical,sory. Critical region = Critical section. Critical region is called so in kernel mode.. but it is the same. – Brans Ds Jul 14 '14 at 10:36
-
What is an [APC](http://msdn.microsoft.com/en-us/library/windows/desktop/ms681951(v=vs.85).aspx)? – didierc Jul 14 '14 at 10:41
-
As described in MSDN: `A thread that is inside a critical region executes with user APCs and normal kernel APCs disabled. A thread inside a guarded region runs with all APCs disabled` http://msdn.microsoft.com/en-us/library/windows/hardware/ff542925%28v=vs.85%29.aspx It is not general programming concept, just used to describe specific kernel mode features. – Alex F Jul 14 '14 at 10:43
-
@AlexFarber So, question, what is the difference betwin then? – Brans Ds Jul 14 '14 at 10:46
-
1@didierc http://msdn.microsoft.com/en-us/library/windows/hardware/ff564853(v=vs.85).aspx – Brans Ds Jul 14 '14 at 10:47
1 Answers
Recall that a process on any modern OS is made of several components. The ones we're interested in are:
- code: the body of the program being executed,
- memory: code, as well as execution related data (heap and stack) are stored there for the duration of a process,
- thread: an execution context, ie CPU state and memory bits (stack for instance) related to that context,
- signal slots: a process structure holding signals emitted by threads to each other, each thread has one such structure (ie, it's one of the memory bits of a thread)
Within a process, there may be several instances of these objects; usually code and memory are accessible by all the existing threads within a process. At any time there may be as many active threads (ie, executing concurrently) as there are available cores on your CPUs. These threads, if they belong to a same process, might interfer when accessing the same memory or code sections. For that purpose, Windows implements the so called critical sections, which are basically protected code blocks (it is very similar to the concept of synchronized code blocks in Java).
However, threads may also be diverted from their current execution code path when a signal is triggered or posted to them. On Windows, APCs are one form of that signaling mechanism. Guarded sections are available to make sure that a thread will complete a given code block before being able to handle these signals (APC in this case).
So, while a critical section will prevent any other thread than the active one to execute the protected code section concurrently, a guarded section will ensure that the current thread will not execute any other code than the guarded one once it started it.
As a simple analogy, imagine a code section like a flat in a building (the process code). A person (thread) who enters a critically protected code will lock the flat's main door, thus preventing any other person to enter it while she's still inside. If the code is guarded, then the OS will lock the person's cellphone, preventing her from answering calls until she actually leaves the flat.
A typical scenario for critical sections is when a specific resource needs to be accessed exclusively (a socket, an file handle, an in memory data structure). For APCs, similarly, guarded sections would prevent a thread from interfering with itself by trying to access one such resource in an APC execution when it was already using it in its current execution code path.
-
there is absent only one important thing. When to use Critical Section, and when Guarded Region? – Brans Ds Jul 14 '14 at 11:51