496 31.Producer‐ConsumerQueues
spinning until they are not empty. To modify these methods to return an error
code instead, replace
continue with return at the appropriate locations. This is
a better strategy when data is being inserted and removed from the queue sporad-
ically, since the application can handle the best way to wait. For example, the
application can call
SwitchToThread() to allow the OS thread scheduler to yield
execution to another thread instead of pegging the processor at 100% with un-
necessary spinning.
31.9InterprocessCommunication
Both the semaphore-based model and the lock-free model can be extended for
use for interprocess communication. Win32 memory-mapped files can act as
shared storage for all the volatile and constant memory used by the algorithms.
To extend to the semaphore-based queue, replace the critical sections with named
mutexes and the semaphores with named semaphores. The queue will be man-
aged using kernel locks, and the head and tail offsets can be maintained as re-
served space in the memory-mapped file.
The lock-free implementation is handled similarly, except no locks are need-
ed, even across processes. However, just like the semaphore-based queue, the
lock-free queue requires all producers and consumers to use the same lock-free
code to ensure thread safety.
From the perspective of both algorithms, there are no differences between an
array in one process and the same array remapped in different processes, since
each algorithm is designed to operate using array indices instead of pointers.
References
[AMD 2010] AMD. AMD64 Architecture Programmer’s Manual Volume 2: System
Programming. Advanced Micro Devices, 2010. Available at http://support.amd.
com/us/Processor_TechDocs/24593.pdf.
[Colvin and Groves 2005] Robert Colvin and Lindsay Groves. “Formal Verification of
an Array-Based Nonblocking Queue.” Proceedings of Engineering of Complex
Computer Systems, June 2005.
[Dawson 2008] Bruce Dawson. “Lockless Programming Considerations for Xbox 360
and Microsoft Windows.” MSDN, June 2008. Available at http://msdn.microsoft.
com/en-us/library/ee418650%28VS.85%29.aspx.