// QueuedMessages.cpp #include #include #include #include "BlockingQueue.h" #include "Threads.h" #include "locks.h" template std::string convert(T t) { std::ostringstream temp; temp << t; return temp.str(); } class ChildThreadProc : public Thread_Processing { public: ChildThreadProc(BQueue& Q) : Q_(Q) {} void run() { size_t count = 0; std::string msg; do { std::string msgHeader = std::string("msg #") + convert(++count) + " "; msg = Q_.deQ(); sout << locker << "\n dequeued " << msgHeader << msg << unlocker; } while(msg != "quit"); sout << "\n Child thread quitting"; } private: BQueue& Q_; // note reference }; void main() { std::cout << "\n Demonstrating Queued Message Passing"; std::cout << "\n ======================================\n"; BQueue Q; ChildThreadProc tp(Q); thread t(tp); t.start(); std::string msgs[5] = { "first", "second", "third", "fourth", "quit" }; for(size_t i=0; i<5; ++i) { sout << locker << "\n enqueuing msg " << msgs[i] << unlocker; Q.enQ(msgs[i]); } t.wait(); sout << "\n Application quitting\n\n"; }