From 3a29d23a563f7399a94716c25166521868297d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eberhard=20Gr=C3=A4ther?= Date: Wed, 19 Feb 2020 09:06:25 +0100 Subject: [PATCH] src: Fixed QtThreadedFunctor gets sometimes stuck (#926) * It happens when executed both from Qt and TaskScheduler thread in parallel. * I found that the signal within QtThreadedFunctorHelper::operator() was sometimes not emitted and therefore QtThreadedFunctorHelper::execute() was never called. --- src/lib_gui/qt/utility/QtThreadedFunctor.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib_gui/qt/utility/QtThreadedFunctor.h b/src/lib_gui/qt/utility/QtThreadedFunctor.h index 8edbbcb3..42e80ebe 100644 --- a/src/lib_gui/qt/utility/QtThreadedFunctor.h +++ b/src/lib_gui/qt/utility/QtThreadedFunctor.h @@ -1,12 +1,11 @@ #ifndef QT_THREADED_FUCTOR_H #define QT_THREADED_FUCTOR_H -#include #include -#include #include #include +#include #include "MessageListener.h" #include "MessageWindowClosed.h" @@ -23,19 +22,30 @@ signals: private slots: void execute() { - m_callback(); + std::function callback = m_callback; m_freeCallbacks.release(); + callback(); } public: QtThreadedFunctorHelper(): m_freeCallbacks(1) { QObject::connect( - this, &QtThreadedFunctorHelper::signalExecution, this, &QtThreadedFunctorHelper::execute); + this, + &QtThreadedFunctorHelper::signalExecution, + this, + &QtThreadedFunctorHelper::execute, + Qt::QueuedConnection); } void operator()(std::function callback) { + if (QThread::currentThread() == this->thread()) + { + callback(); + return; + } + m_freeCallbacks.acquire(); m_callback = callback; emit signalExecution();