summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qthread.cpp
diff options
context:
space:
mode:
authorCorentin Jabot <corentinjabot@gmail.com>2013-01-20 13:24:30 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-09-05 08:20:18 +0200
commit5a02d30a78991240c3355d863f52b0d376ebf911 (patch)
treead02a7281b44f452016bae6a31ec3a25f8905596 /src/corelib/thread/qthread.cpp
parent34195aa6cbc3bbe2b0bb38a86a3fb3ad76c40499 (diff)
Add an advisory interruption mechanism to QThread.
To ease interruption of long running tasks, a new method QThread::setInterruptionRequested() can be called. The task can check QThread::isInterruptionRequested() and act upon it by stopping itself. These methods are designed to replace the use of a global variable and other hacky ways to stop a task running in another thread. Change-Id: I17622dd60d2262078210e7e4294ad6c53a6dc179 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/thread/qthread.cpp')
-rw-r--r--src/corelib/thread/qthread.cpp60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp
index 4d5bee3154..35d57b3d83 100644
--- a/src/corelib/thread/qthread.cpp
+++ b/src/corelib/thread/qthread.cpp
@@ -146,7 +146,8 @@ void QAdoptedThread::run()
QThreadPrivate::QThreadPrivate(QThreadData *d)
: QObjectPrivate(), running(false), finished(false),
- isInFinish(false), exited(false), returnCode(-1),
+ isInFinish(false), interruptionRequested(false),
+ exited(false), returnCode(-1),
stackSize(0), priority(QThread::InheritPriority), data(d)
{
#if defined (Q_OS_UNIX)
@@ -801,4 +802,61 @@ bool QThread::event(QEvent *event)
}
}
+/*!
+ \since 5.2
+
+ Request the interruption of the thread.
+ That request is advisory and it is up to code running on the thread to decide
+ if and how it should act upon such request.
+ This function does not stop any event loop running on the thread and
+ does not terminate it in any way.
+
+ \sa isInterruptionRequested()
+*/
+
+void QThread::requestInterruption()
+{
+ Q_D(QThread);
+ QMutexLocker locker(&d->mutex);
+ if (!d->running || d->finished || d->isInFinish)
+ return;
+ if (this == QCoreApplicationPrivate::theMainThread) {
+ qWarning("QThread::requestInterruption has no effect on the main thread");
+ return;
+ }
+ d->interruptionRequested = true;
+}
+
+/*!
+ \since 5.2
+
+ Return true if the task running on this thread should be stopped.
+ An interruption can be requested by requestInterruption().
+
+ This function can be used to make long running tasks cleanly interruptible.
+ Never checking or acting on the value returned by this function is safe,
+ however it is advisable do so regularly in long running functions.
+ Take care not to call it too often, to keep the overhead low.
+
+ \code
+ void long_task() {
+ forever {
+ if ( QThread::currentThread()->isInterruptionRequested() ) {
+ return;
+ }
+ }
+ }
+ \endcode
+
+ \sa currentThread() requestInterruption()
+*/
+bool QThread::isInterruptionRequested() const
+{
+ Q_D(const QThread);
+ QMutexLocker locker(&d->mutex);
+ if (!d->running || d->finished || d->isInFinish)
+ return false;
+ return d->interruptionRequested;
+}
+
QT_END_NAMESPACE