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>();
|
||||
|
||||
@@ -43,11 +43,13 @@ private:
|
||||
size_t m_currentListenerIndex;
|
||||
size_t m_listenersLength;
|
||||
bool m_loopIsRunning;
|
||||
bool m_threadIsRunning;
|
||||
|
||||
mutable std::mutex m_frontMessageBufferMutex;
|
||||
mutable std::mutex m_backMessageBufferMutex;
|
||||
mutable std::mutex m_listenersMutex;
|
||||
mutable std::mutex m_loopMutex;
|
||||
mutable std::mutex m_threadMutex;
|
||||
};
|
||||
|
||||
#endif // MESSAGE_QUEUE_H
|
||||
|
||||
@@ -31,6 +31,9 @@ void TaskScheduler::interruptCurrentTask()
|
||||
void TaskScheduler::startSchedulerLoopThreaded()
|
||||
{
|
||||
std::thread(&TaskScheduler::startSchedulerLoop, this).detach();
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||
m_threadIsRunning = true;
|
||||
}
|
||||
|
||||
void TaskScheduler::startSchedulerLoop()
|
||||
@@ -49,20 +52,28 @@ void TaskScheduler::startSchedulerLoop()
|
||||
|
||||
while (true)
|
||||
{
|
||||
updateTasks();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_loopMutex);
|
||||
|
||||
if (!m_loopIsRunning)
|
||||
{
|
||||
return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
updateTasks();
|
||||
|
||||
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 TaskScheduler::stopSchedulerLoop()
|
||||
@@ -79,6 +90,20 @@ void TaskScheduler::stopSchedulerLoop()
|
||||
}
|
||||
|
||||
interruptCurrentTask();
|
||||
|
||||
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 TaskScheduler::loopIsRunning() const
|
||||
@@ -91,6 +116,7 @@ std::shared_ptr<TaskScheduler> TaskScheduler::s_instance;
|
||||
|
||||
TaskScheduler::TaskScheduler()
|
||||
: m_loopIsRunning(false)
|
||||
, m_threadIsRunning(false)
|
||||
, m_interruptTask(false)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -36,12 +36,14 @@ private:
|
||||
virtual void handleMessage(MessageInterruptTasks* message);
|
||||
|
||||
bool m_loopIsRunning;
|
||||
bool m_threadIsRunning;
|
||||
|
||||
std::queue<std::shared_ptr<Task>> m_tasks;
|
||||
bool m_interruptTask;
|
||||
|
||||
mutable std::mutex m_tasksMutex;
|
||||
mutable std::mutex m_loopMutex;
|
||||
mutable std::mutex m_threadMutex;
|
||||
};
|
||||
|
||||
#endif // TASK_SCHEDULER_H
|
||||
|
||||
Reference in New Issue
Block a user