From 42f228e75afcea53080de569d932100288d13240 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 23 Apr 2014 12:17:14 -0400 Subject: [PATCH] proper termination --- Queue.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Queue.h b/Queue.h index 85114bc1..52474aff 100644 --- a/Queue.h +++ b/Queue.h @@ -46,6 +46,12 @@ namespace util return el; } + void Wait () + { + std::unique_lock l(m_QueueMutex); + m_NonEmpty.wait (l); + } + bool Wait (int sec, int usec) { std::unique_lock l(m_QueueMutex); @@ -98,27 +104,33 @@ namespace util { public: - MsgQueue (): m_Thread (std::bind (&MsgQueue::Run, this)) , running(1) {}; + MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue::Run, this)) {}; void Stop() { - running = 0; + m_IsRunning = false; + Queue::WakeUp (); m_Thread.join(); } private: + void Run () { - Msg * msg = nullptr; - while ((msg = Queue::GetNext ()) != nullptr && running) + while (m_IsRunning) { - msg->Process (); - delete msg; + while (Msg * msg = Queue::Get ()) + { + msg->Process (); + delete msg; + } + Queue::Wait (); } } private: - std::thread m_Thread; - volatile int running; + + bool m_IsRunning; + std::thread m_Thread; }; } }