///////////////////////////////////////////////////////////////// // QueuedMessages.cpp - communication between threads using a // // blockingQueue // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2012 // ///////////////////////////////////////////////////////////////// #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; ::Sleep(200); } 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(); const size_t NUMMSGS = 7; std::string msgs[NUMMSGS] = { "first", "second", "third", "fourth", "fifth", "sixth", "quit" }; for(size_t i=0; i