From bd5c6fab403fd8d79b84aaf05424a24e2c88709d Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Mon, 23 Oct 2023 13:41:46 +0200 Subject: [PATCH] QueuedCustomType example: use QThread::requestInterruption Drop the home-made solution with mutex and bool (which could have been an atomic, but we have had a ready-made solution in QThread for a long time). Pick-to: 6.5 6.6 Change-Id: Id213a021f0ae94215afb28ff874fcb597dd1e6f9 Reviewed-by: MohammadHossein Qanbari Reviewed-by: Edward Welbourne Reviewed-by: Mikko Hallamaa Reviewed-by: Ed Cooke --- .../threads/queuedcustomtype/renderthread.cpp | 15 +-------------- .../threads/queuedcustomtype/renderthread.h | 4 ---- .../corelib/threads/queuedcustomtype/window.cpp | 2 +- 3 files changed, 2 insertions(+), 19 deletions(-) diff --git a/examples/corelib/threads/queuedcustomtype/renderthread.cpp b/examples/corelib/threads/queuedcustomtype/renderthread.cpp index 5a65f4ca76c..34a439f3fe2 100644 --- a/examples/corelib/threads/queuedcustomtype/renderthread.cpp +++ b/examples/corelib/threads/queuedcustomtype/renderthread.cpp @@ -10,15 +10,10 @@ RenderThread::RenderThread(QObject *parent) : QThread(parent) { - m_abort = false; } RenderThread::~RenderThread() { - mutex.lock(); - m_abort = true; - mutex.unlock(); - wait(); } @@ -29,7 +24,6 @@ void RenderThread::processImage(const QImage &image) return; m_image = image; - m_abort = false; start(); } @@ -60,17 +54,10 @@ void RenderThread::run() const Block block(QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1), QColor(red/n, green/n, blue/n)); emit sendBlock(block); - if (m_abort) + if (isInterruptionRequested()) return; msleep(10); } } } //![processing the image (finish)] - -void RenderThread::stopProcess() -{ - mutex.lock(); - m_abort = true; - mutex.unlock(); -} diff --git a/examples/corelib/threads/queuedcustomtype/renderthread.h b/examples/corelib/threads/queuedcustomtype/renderthread.h index ed4df99bcc3..c3152e40810 100644 --- a/examples/corelib/threads/queuedcustomtype/renderthread.h +++ b/examples/corelib/threads/queuedcustomtype/renderthread.h @@ -5,7 +5,6 @@ #define RENDERTHREAD_H #include -#include #include class Block; @@ -20,7 +19,6 @@ public: ~RenderThread(); void processImage(const QImage &image); - void stopProcess(); signals: void sendBlock(const Block &block); @@ -29,9 +27,7 @@ protected: void run(); private: - bool m_abort; QImage m_image; - QMutex mutex; }; //! [RenderThread class definition] diff --git a/examples/corelib/threads/queuedcustomtype/window.cpp b/examples/corelib/threads/queuedcustomtype/window.cpp index 9a56007b40b..db121332067 100644 --- a/examples/corelib/threads/queuedcustomtype/window.cpp +++ b/examples/corelib/threads/queuedcustomtype/window.cpp @@ -29,7 +29,7 @@ Window::Window(QWidget *parent) connect(loadButton, &QPushButton::clicked, this, QOverload<>::of(&Window::loadImage)); connect(resetButton, &QPushButton::clicked, - thread, &RenderThread::stopProcess); + thread, &RenderThread::requestInterruption); connect(thread, &RenderThread::finished, this, &Window::resetUi); //! [set up widgets and connections] //! [connecting signal with custom type]