+ 1

Why producer consumer not working at all

Hi Refer code below: It does hit break point in producer and consumer function But why there is no output on console. Am I missing something? https://sololearn.com/compiler-playground/cvmF60ickNx8/?ref=app

12th Mar 2025, 7:57 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Réponses
+ 3
maybe something like this? rearrange the consumer method void consumer() { ... // move this up here int val = q.front(); cout << val << endl; if (isProductionDone) { cv.notify_all(); break; } ... } The producers don't cout anything, only consumers have cout, and you only have 2 consumers. also you're not popping from the queue. all consumers will end up just displaying the same q.front(). https://sololearn.com/compiler-playground/cczEjkK7K8o2/?ref=app
13th Mar 2025, 9:09 AM
Bob_Li
Bob_Li - avatar
+ 1
Thanks a lot ..! I was consumer thread stopping as soon as production is done. Which is not the right approach. if (isProductionDone)//So, as you pointed out, this is the culprit. if (isProductionDone && q.empty()) //This should be proper option Now then your point of q.pop is also missing point. Thanks again for finding this issue.
13th Mar 2025, 12:58 PM
Ketan Lalcheta
Ketan Lalcheta - avatar