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/corelib/thread/qthread.cpp | |
| 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/corelib/thread/qthread.cpp')
| -rw-r--r-- | src/corelib/thread/qthread.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index a9115c5b397..2cac443e53f 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; |
