/////////////////////////////////////////////////////////////// // Cpp11-BlockingQueue.cpp - Thread-safe Blocking Queue // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2013 // /////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include "Cpp11-BlockingQueue.h" std::mutex ioLock; void test(BlockingQueue* pQ) { std::string msg; do { msg = pQ->deQ(); { std::lock_guard l(ioLock); std::cout << "\n thread deQed " << msg.c_str(); } std::this_thread::sleep_for(std::chrono::milliseconds(10)); } while(msg != "quit"); } int main() { std::cout << "\n Demonstrating C++11 Blocking Queue"; std::cout << "\n ===================================="; BlockingQueue q; std::thread t(test, &q); for(int i=0; i<25; ++i) { std::ostringstream temp; temp << i; std::string msg = std::string("msg#") + temp.str(); { std::lock_guard l(ioLock); std::cout << "\n main enQing " << msg.c_str(); } q.enQ(msg); std::this_thread::sleep_for(std::chrono::milliseconds(3)); } t.join(); }