#include #include #include #include #include #include using namespace std; mutex amutex; condition_variable is_not_full; condition_variable is_not_empty; int main() { int c = 0; bool done = false; queue goods; thread producer([&]() { for (int i = 0; i < 500; ++i) { unique_lock lock(amutex); // wait w/ func call as 2nd argument, wait only executed if false returned is_not_full.wait(lock, [&goods](){return goods.empty(); }); goods.push(i); c++; //cout << "p: " << c << " i=" << i << endl; is_not_empty.notify_one(); } done = true; }); thread consumer([&]() { while (!done) { while (!goods.empty()) { unique_lock lock(amutex); is_not_empty.wait(lock, [&goods](){return !goods.empty(); }); goods.pop(); c--; //cout << "c: " << c << endl; is_not_full.notify_one(); } } }); producer.join(); consumer.join(); cout << "Net: " << c << endl; }