utility: Wait until threads stopped when stopping loops of MessageQueue or TaskScheduler
This commit is contained in:
@@ -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>();
|
||||
|
||||
Reference in New Issue
Block a user