Producer and Consumer Processes

The following programming example demonstrates the use of some of the mutex and condition variable functions in solving the bounded buffer problem. This is accomplished by implementing a monitor with these functions.

In Figure 1, a set of producer threads supply messages to a set of consumer threads.
Figure 1. Producer Threads Supply Messages to Consumer Threads
    Producer: Procedure
        .
        .
    Repeat
         Create a new message

         /* Add the new message to the buffer */
         Call Write(message)
         .
    Until DONE


    Consumer: Procedure
         .
         .
    Repeat
          /* Consume a message from the buffer */
          Call Read(message)
          .
    Until DONE