From the course: C++ Development: Advanced Concepts, Lambda Expressions, and Best Practices

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

The producer-consumer idiom

The producer-consumer idiom

- [Instructor] The producer-consumer idiom is where one process produces data, and another consumes data using one container to hold the data. This requires coordination between the producer and consumer threads to manage the container and prevent unwanted side effects. This is producer-consumer.cpp from chapter six of the exercise files, and you'll see that we have a number of variables in global space. If you're concerned about scope and name collisions, you could put these in a name space. The mutexs are used for controlling access to the data between the producer and the consumer. The deque is a queue used for passing the data from producer to consumer. And the atomic_flag is used to notify the consumer when the producer is finished sending data. Both the producer and consumer threads, and here's the functions for them, they both use a lock guard because it's safe and convenient, and it automatically releases its…

Contents