// // 7.5.producer.consumer.cpp // chapter 7 parallelism and concurrency // modern c++ tutorial // // created by changkun at changkun.de // https://github.com/changkun/modern-cpp-tutorial // #include #include #include #include #include #include int main() { std::queue produced_nums; std::mutex mtx; std::condition_variable cv; bool notified = false; // notification sign auto producer = [&]() { for (int i = 0; ; i++) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::unique_lock lock(mtx); std::cout << "producing " << i << std::endl; produced_nums.push(i); notified = true; cv.notify_all(); } }; auto consumer = [&]() { while (true) { std::unique_lock lock(mtx); while (!notified) { // avoid spurious wakeup cv.wait(lock); } // temporal unlock to allow producer produces more rather than // let consumer hold the lock until its consumed. lock.unlock(); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // consumer is slower lock.lock(); if (!produced_nums.empty()) { std::cout << "consuming " << produced_nums.front() << std::endl; produced_nums.pop(); } notified = false; } }; std::thread p(producer); std::thread cs[2]; for (int i = 0; i < 2; ++i) { cs[i] = std::thread(consumer); } p.join(); for (int i = 0; i < 2; ++i) { cs[i].join(); } return 0; }