diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2023-01-25 12:53:32 +0100 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2024-04-26 18:19:39 +0200 |
commit | 7a374c38d288435b3c0a76b82a1c2ca53ea9c003 (patch) | |
tree | 77935a10880cbb4a8198a8a6700a4065369b3e66 /src | |
parent | 63f4708a9995af73a34986658fe4f0d2c1512588 (diff) |
QThread: Introduce isCurrentThread
This allows a more efficient way of checking whether a thread is the
currently executing one (without using private API).
Change-Id: I007edae6b258d7e42e901fa720d4f3cf9fe25a49
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/thread/qthread.cpp | 35 | ||||
-rw-r--r-- | src/corelib/thread/qthread.h | 2 | ||||
-rw-r--r-- | src/corelib/thread/qthread_p.h | 1 |
3 files changed, 38 insertions, 0 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index a9115c5b39..2cac443e53 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -913,6 +913,36 @@ int QThread::loopLevel() const return d->data->eventLoops.size(); } +/*! + \internal + Returns the thread handle of this thread. + It can be compared with the return value of currentThreadId(). + + This is used to implement isCurrentThread, and might be useful + for debugging (e.g. by comparing the value in gdb with info threads). + + \note Thread handles of destroyed threads might be reused by the + operating system. Storing the return value of this function can + therefore give surprising results if it outlives the QThread object + (threads claimed to be the same even if they aren't). +*/ +Qt::HANDLE QThreadPrivate::threadId() const +{ + return data->threadId.loadRelaxed(); +} + +/*! + \since 6.8 + Returns true if this thread is QThread::currentThread. + + \sa currentThreadId() +*/ +bool QThread::isCurrentThread() const +{ + Q_D(const QThread); + return QThread::currentThreadId() == d->threadId(); +} + #else // QT_CONFIG(thread) QThread::QThread(QObject *parent) @@ -985,6 +1015,11 @@ QThread *QThread::currentThread() return QThreadData::current()->thread.loadAcquire(); } +bool QThread::isCurrentThread() const +{ + return true; +} + int QThread::idealThreadCount() noexcept { return 1; diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h index a4b7183b5a..fcef6deb59 100644 --- a/src/corelib/thread/qthread.h +++ b/src/corelib/thread/qthread.h @@ -68,6 +68,8 @@ public: bool event(QEvent *event) override; int loopLevel() const; + bool isCurrentThread() const; + template <typename Function, typename... Args> [[nodiscard]] static QThread *create(Function &&f, Args &&... args); diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h index 2335ba398d..c39e21ec9a 100644 --- a/src/corelib/thread/qthread_p.h +++ b/src/corelib/thread/qthread_p.h @@ -179,6 +179,7 @@ public: ~QThreadPrivate(); void setPriority(QThread::Priority prio); + Qt::HANDLE threadId() const; mutable QMutex mutex; QAtomicInt quitLockRef; |