Chapter 10
Over the years, I've done a lot of work with thread synchronization and have written some C++ classes and components that I'll share with you in this chapter. I hope you'll find this code useful and that it will save you a lot of development time. At the very least, I hope that you learn something from what I offer here.
I'll begin the chapter by showing you how to implement a critical section and add features to it. In particular, you'll learn how to use a critical section in multiple processes. Then you'll learn how to wrap your own datatypes in a C++ class so that the objects are thread-safe. Using these classes, I'll also present an object that behaves opposite of a semaphore.
Next, we'll look at how to solve a common programming problem—when you have multiple threads reading a resource and only one thread writing to a resource. Windows has no built-in primitive that makes this type of synchronization easy, so I wrote a C++ class to do this.
Finally, I'll show you how to implement my WaitForMultipleExpressions function, which lets you create complex expressions to indicate when a thread should wake. (It works much like the WaitForMultipleObjects function, which lets you wait for any single object to be signaled or for all objects to be signaled.)