Browse Source

proper termination

pull/70/head
orignal 11 years ago
parent
commit
42f228e75a
  1. 22
      Queue.h

22
Queue.h

@ -46,6 +46,12 @@ namespace util
return el; return el;
} }
void Wait ()
{
std::unique_lock<std::mutex> l(m_QueueMutex);
m_NonEmpty.wait (l);
}
bool Wait (int sec, int usec) bool Wait (int sec, int usec)
{ {
std::unique_lock<std::mutex> l(m_QueueMutex); std::unique_lock<std::mutex> l(m_QueueMutex);
@ -98,27 +104,33 @@ namespace util
{ {
public: public:
MsgQueue (): m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) , running(1) {}; MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {};
void Stop() void Stop()
{ {
running = 0; m_IsRunning = false;
Queue<Msg>::WakeUp ();
m_Thread.join(); m_Thread.join();
} }
private: private:
void Run () void Run ()
{ {
Msg * msg = nullptr; while (m_IsRunning)
while ((msg = Queue<Msg>::GetNext ()) != nullptr && running) {
while (Msg * msg = Queue<Msg>::Get ())
{ {
msg->Process (); msg->Process ();
delete msg; delete msg;
} }
Queue<Msg>::Wait ();
}
} }
private: private:
bool m_IsRunning;
std::thread m_Thread; std::thread m_Thread;
volatile int running;
}; };
} }
} }

Loading…
Cancel
Save