Using Critical Sections

This document describes how to use critical sections.

Critical sections are provided to serialise access to shared resources. They are Kernel objects and, as such, are managed by the Kernel.

A critical section can only be used by threads within the same process; it cannot be used to control access to a resource shared by threads across different processes. To serialise access to a resource shared by threads across more than one process, use a mutex.

While a critical section is used to serialise access to a general resource, it is used, typically, to serialise access to a section of re-entrant code which cannot be executed concurrently by more than one thread within a process. A critical section only allows one thread into a controlled section, forcing other threads, that try to gain access to that critical section, to wait until the first thread has exited from that section.

Note that threads which do not attempt to gain access to the critical section are not blocked.

A critical section is implemented using a semaphore.

Access to a critical section object is through an RCriticalSection handle.