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>
This commit is contained in:
parent
63f4708a99
commit
7a374c38d2
@ -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;
|
||||
|
@ -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);
|
||||
|
||||
|
@ -179,6 +179,7 @@ public:
|
||||
~QThreadPrivate();
|
||||
|
||||
void setPriority(QThread::Priority prio);
|
||||
Qt::HANDLE threadId() const;
|
||||
|
||||
mutable QMutex mutex;
|
||||
QAtomicInt quitLockRef;
|
||||
|
@ -137,11 +137,13 @@ class Current_Thread : public QThread
|
||||
public:
|
||||
Qt::HANDLE id;
|
||||
QThread *thread;
|
||||
bool runCalledInCurrentThread = false;
|
||||
|
||||
void run() override
|
||||
{
|
||||
id = QThread::currentThreadId();
|
||||
thread = QThread::currentThread();
|
||||
runCalledInCurrentThread = thread->isCurrentThread();
|
||||
}
|
||||
};
|
||||
|
||||
@ -276,6 +278,11 @@ void tst_QThread::currentThreadId()
|
||||
QVERIFY(thread.wait(five_minutes));
|
||||
QVERIFY(thread.id != nullptr);
|
||||
QVERIFY(thread.id != QThread::currentThreadId());
|
||||
QVERIFY(!thread.isCurrentThread());
|
||||
QVERIFY(!thread.thread->isCurrentThread());
|
||||
QVERIFY(thread.QThread::thread()->isCurrentThread());
|
||||
QVERIFY(thread.runCalledInCurrentThread);
|
||||
QVERIFY(qApp->thread()->isCurrentThread());
|
||||
}
|
||||
|
||||
void tst_QThread::currentThread()
|
||||
|
Loading…
x
Reference in New Issue
Block a user