mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-31 00:34:20 +00:00
proper termination
This commit is contained in:
parent
7bee253e46
commit
42f228e75a
28
Queue.h
28
Queue.h
@ -46,6 +46,12 @@ namespace util
|
||||
return el;
|
||||
}
|
||||
|
||||
void Wait ()
|
||||
{
|
||||
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||||
m_NonEmpty.wait (l);
|
||||
}
|
||||
|
||||
bool Wait (int sec, int usec)
|
||||
{
|
||||
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||||
@ -98,27 +104,33 @@ namespace util
|
||||
{
|
||||
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()
|
||||
{
|
||||
running = 0;
|
||||
m_IsRunning = false;
|
||||
Queue<Msg>::WakeUp ();
|
||||
m_Thread.join();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void Run ()
|
||||
{
|
||||
Msg * msg = nullptr;
|
||||
while ((msg = Queue<Msg>::GetNext ()) != nullptr && running)
|
||||
while (m_IsRunning)
|
||||
{
|
||||
msg->Process ();
|
||||
delete msg;
|
||||
while (Msg * msg = Queue<Msg>::Get ())
|
||||
{
|
||||
msg->Process ();
|
||||
delete msg;
|
||||
}
|
||||
Queue<Msg>::Wait ();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::thread m_Thread;
|
||||
volatile int running;
|
||||
|
||||
bool m_IsRunning;
|
||||
std::thread m_Thread;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user