1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-31 04:44:13 +00:00

proper termination

This commit is contained in:
orignal 2014-04-23 12:17:14 -04:00
parent 7bee253e46
commit 42f228e75a

28
Queue.h
View File

@ -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)
{ {
msg->Process (); while (Msg * msg = Queue<Msg>::Get ())
delete msg; {
msg->Process ();
delete msg;
}
Queue<Msg>::Wait ();
} }
} }
private: private:
std::thread m_Thread;
volatile int running; bool m_IsRunning;
std::thread m_Thread;
}; };
} }
} }