From 1c0ef6cd00100db444bcea3d91c10c0d54f8568d Mon Sep 17 00:00:00 2001 From: Matteo Pignataro <matteo.pignataro@skywarder.eu> Date: Sat, 12 Nov 2022 10:14:52 +0100 Subject: [PATCH] [ActiveObject] Bug fix about running flag --- src/shared/ActiveObject.h | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/shared/ActiveObject.h b/src/shared/ActiveObject.h index c26c6b2d9..2d0c2ea82 100644 --- a/src/shared/ActiveObject.h +++ b/src/shared/ActiveObject.h @@ -51,6 +51,8 @@ public: /** * @brief Start the thread associated with this active object. * + * @warning The method is not thread safe. + * * Call stop() to terminate execution of the thread. * @return true if the thread has been started. */ @@ -114,15 +116,16 @@ inline bool ActiveObject::start() if (!running) { stopFlag = false; - thread = miosix::Thread::create(threadLauncher, stackSize, priority, - reinterpret_cast<void*>(this), - miosix::Thread::JOINABLE); - - if (thread != nullptr) - { - running = true; - return true; - } + running = true; + + thread = miosix::Thread::create(threadLauncher, stackSize, priority, + reinterpret_cast<void*>(this), + miosix::Thread::JOINABLE); + + if (thread == nullptr) + running = false; + + return running; } return false; @@ -143,6 +146,7 @@ inline bool ActiveObject::shouldStop() { return stopFlag; } inline void ActiveObject::threadLauncher(void* arg) { + reinterpret_cast<ActiveObject*>(arg)->run(); // When the run function ends, update the status -- GitLab