The Using POSIX semaphores recipe showed us how to use POSIX semaphores to protect a critical region. In this recipe, you'll learn how to use it as a counting semaphore and notification mechanism. We'll do this by developing a classical publish-subscriber program where there is one publisher thread and one consumer thread. The challenge here is that we want to limit the maximum number of items in the queue to a defined value.
POSIX semaphores advanced usage
How to do it...
In this recipe, we'll write a program representing a typical use case for a counting semaphore – a producer-consumer problem in which we want to limit the number of items in the queue to a certain number. Let's get started...