Cook Computing

Cross-Process Critical Sections

December 27, 2003 Written by Charles Cook

It is often thought that to implement cross-process synchronization on a Windows OS you need to use a kernel object such as a mutex which involves a switch to kernel mode, and back to user mode, everytime the lock is acquired and released; in contrast to a critical section which does not incur the overhead of the mode switch if there is no contention for the lock but which can only be used within a process. But, in fact, it is quite possible implement a critical section-like object which works cross-process, the key to this being the use of shared memory. Jeffrey Richter describes just such an object in this article in MSDN Magazine January 1998. This COptex class also contains a configurable spin lock implementation for use on multi-processor systems.

On the project I'm working on at the moment the use of COptext makes a significant difference to CPU usage in certain scenarios.