June 2017
Intermediate to advanced
532 pages
12h 59m
English
In this recipe, we simply started two threads. The first thread produces items and puts them into a queue. The other takes items out of the queue. Whenever one of those threads touches the queue in any way, it locks the common mutex mut which is accessible for both. This way we made sure that it cannot happen that both threads manipulate the queue's state at the same time.
Apart from the queue and the mutex, we declared generally four variables that were involved in the producer-consumer thing:
queue<size_t> q;mutex mut;condition_variable cv;bool finished {false};
The variable finished is easy to explain. It was set to true when the producer finished producing its fixed amount of items. When the consumer sees that this variable ...
Read now
Unlock full access