November 2007
Intermediate to advanced
848 pages
27h 15m
English
A critical section is a small section of code that requires exclusive access to some shared resource before the code can execute. This is a way to have several lines of code "atomically" manipulate a resource. By atomic, I mean that the code knows that no other thread will access the resource. Of course, the system can still preempt your thread and schedule other threads. However, it will not schedule any other threads that want to access the same resource until your thread leaves the critical section.
Here is some problematic code that demonstrates what happens without the use of a critical section:
const int COUNT = 1000; int g_nSum = 0; DWORD WINAPI FirstThread(PVOID pvParam) { g_nSum = 0; for (int n = 1; n <= COUNT; n++) { ...Read now
Unlock full access