From the course: Parallel and Concurrent Programming with C++ Part 2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Producer-consumer: C++ demo

Producer-consumer: C++ demo - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 2

Start my 1-month free trial

Producer-consumer: C++ demo

- [Instructor] To demonstrate a producer-consumer scenario with C++, we've defined a custom class on line seven named ServingLine to pass virtual bowls of soup between our threads. The ServingLine instantiates a queue from the C++ standard library on line 20, which provides basic first in first out or FIFO queue capability. The serve_soup function on line nine simply pushes a new bowl of soup under the queue and the take_soup function on line 13 removes the first bowl of soup from the queue and returns its value. We're simply using integers here to represent the bowls of soup. Scrolling down on line 23, we instantiate a global serving_line variable for all of our threads to interact with. And then on line 25, we define the soup_producer function. It uses a for loop to serve 10,000 bowls of soup, which are represented by the integer value one. Then, after the for loop, it places a negative one into the serving queue on line…

Contents