diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-10-23 13:41:46 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-10-27 07:55:42 +0200 |
commit | bd5c6fab403fd8d79b84aaf05424a24e2c88709d (patch) | |
tree | 05561d620d9db43d9432f14636b316085a9b86f3 /examples/corelib | |
parent | ae39b1634556f82fe5d7505ed9b6ebb883d6f813 (diff) |
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 <mohammad.qanbari@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mikko Hallamaa <mikko.hallamaa@qt.io>
Reviewed-by: Ed Cooke
Diffstat (limited to 'examples/corelib')
3 files changed, 2 insertions, 19 deletions
diff --git a/examples/corelib/threads/queuedcustomtype/renderthread.cpp b/examples/corelib/threads/queuedcustomtype/renderthread.cpp index 5a65f4ca76..34a439f3fe 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 ed4df99bcc..c3152e4081 100644 --- a/examples/corelib/threads/queuedcustomtype/renderthread.h +++ b/examples/corelib/threads/queuedcustomtype/renderthread.h @@ -5,7 +5,6 @@ #define RENDERTHREAD_H #include <QImage> -#include <QMutex> #include <QThread> 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 9a56007b40..db12133206 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] |