Synchronizing with multiple workers
In the previous section, we implemented a very simple synchronization mechanism that was used by a thread to get notifications about new messages – waiting on an event. This is simple and effective but not very flexible.
When we write complex systems where one thread starts multiple worker threads, this simple approach usually fails. We need something better, a solution that can wait on multiple events and do something when (any) one of them triggers. Windows offers a great tool for that – the WaitForMultipleObjects
API function (which comes in several variations) – but that locks us into one platform. What can we do if we need to run our programs on other operating systems?
A good answer to that question is condition variables. They are, however, somehow weirdly implemented in the Delphi RTL (more on that later), and they remain a total mystery to most Windows Delphi programmers. To help anyone that wants to dive into...