Programming with Hyper-Threading Technology: How to Write Multithreaded Software for Intel IA-32 Processors

The Windows threading API has a unique mechanism called a critical section. The choice of terms is unfortunate, because critical section refers in Pthreads and other threading APIs to a section of code that is protected by some mechanism, such as a mutex. In Windows, however, it refers to the mechanism itself.
The critical section is, in essence, a high-speed mutex. The way Windows implements the mutex is by making a system call; that is, it calls kernel code. This aspect has the advantage that mutexes have system-wide visibility. Code in a different process, that is, a completely different program, can access a mutex that you've created. The disadvantage is that the switch Windows must make to kernel mode to handle the mutex call is very expensive in terms of performance. Just how expensive will be highlighted shortly.
Because programs frequently have no desire to share mutexes across processes, but do have a lively interest in performance, Microsoft developed the mechanism of the critical section. It's a mutex mechanism that operates in the user space. It makes no system call and is extremely fast.
In addition, the critical section is simple to code. You declare the critical section, which is an opaque data type. You initialize it. Then you enter and exit the critical sections. If a thread tries to enter a section guarded by a critical section that is already locked, the thread waits until the critical section becomes available. In effect, the critical section will...