utility: Wait until threads stopped when stopping loops of MessageQueue or TaskScheduler

This commit is contained in:
Eberhard Graether
2015-04-24 16:18:13 +02:00
parent d56a9afe24
commit abd29b1060
4 changed files with 69 additions and 11 deletions
+36 -8
View File
@@ -70,6 +70,9 @@ void MessageQueue::pushMessage(std::shared_ptr<MessageBase> message)
void MessageQueue::startMessageLoopThreaded()
{
std::thread(&MessageQueue::startMessageLoop, this).detach();
std::lock_guard<std::mutex> lock(m_threadMutex);
m_threadIsRunning = true;
}
void MessageQueue::startMessageLoop()
@@ -88,32 +91,56 @@ void MessageQueue::startMessageLoop()
while (true)
{
processMessages();
{
std::lock_guard<std::mutex> lock(m_loopMutex);
if (!m_loopIsRunning)
{
return;
break;
}
}
processMessages();
const int SLEEP_TIME_MS = 25;
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
}
{
std::lock_guard<std::mutex> lock(m_threadMutex);
if (m_threadIsRunning)
{
m_threadIsRunning = false;
}
}
}
void MessageQueue::stopMessageLoop()
{
std::lock_guard<std::mutex> lock(m_loopMutex);
if (!m_loopIsRunning)
{
LOG_WARNING("Loop is not running");
std::lock_guard<std::mutex> lock(m_loopMutex);
if (!m_loopIsRunning)
{
LOG_WARNING("Loop is not running");
}
m_loopIsRunning = false;
}
m_loopIsRunning = false;
while (true)
{
{
std::lock_guard<std::mutex> lock(m_threadMutex);
if (!m_threadIsRunning)
{
break;
}
}
const int SLEEP_TIME_MS = 25;
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
}
}
bool MessageQueue::loopIsRunning() const
@@ -135,6 +162,7 @@ MessageQueue::MessageQueue()
: m_currentListenerIndex(0)
, m_listenersLength(0)
, m_loopIsRunning(false)
, m_threadIsRunning(false)
{
m_frontMessageBuffer = std::make_shared<MessageBufferType>();
m_backMessageBuffer = std::make_shared<MessageBufferType>();