#ifndef MSGQUEUETHREAD_H #define MSGQUEUETHREAD_H #include #include #include #include #include using namespace std; //TODO:实现移到cpp中 template class MsgQueueThread { typedef function)> Handler; public: void setHandler(Handler handler) { mHandler = handler; } void start() { if (mRun) return; mRun=true; thread thd(&MsgQueueThread::loop, this); mThread.swap(thd); } void stop() { if (!mRun) return; mRun=false; unique_lock lock(mQueueMutex); while(!mQueue.empty()) mQueue.pop(); mQueueCnd.notify_all(); lock.unlock(); mThread.join(); } void sendMessage(shared_ptr msg) { unique_lock lock(mQueueMutex); mQueue.push(msg); mQueueCnd.notify_one(); } private: void loop() { while (mRun) { unique_lock lock(mQueueMutex); if (mQueue.empty()) mQueueCnd.wait(lock); if (!mRun) return; auto msg = mQueue.front(); mQueue.pop(); lock.unlock();//队列已经没有利用价值了,放了它 if (mHandler) mHandler(msg); } } private: condition_variable mQueueCnd; mutex mQueueMutex; queue> mQueue; bool mRun=false; Handler mHandler; thread mThread; }; #endif // MSGQUEUETHREAD_H