1.int loopLevel() const;
int QThread::loopLevel() const
{
Q_D(const QThread);
return d->data->eventLoops.size();
}
返回当前线程的looplevel. QThread::exec()会增加looplevel计数。
QThread::Priority QThread::priority() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
// mask off the high bits that are used for flags
return Priority(d->priority & 0xffff);
}
返回线程的优先级。
3. void requestInterruption();
void QThread::requestInterruption()
{
if (this == QCoreApplicationPrivate::theMainThread.loadAcquire()) {
qWarning("QThread::requestInterruption has no effect on the main thread");
return;
}
Q_D(QThread);
// ### Qt 6: use std::atomic_flag, and document that
// requestInterruption/isInterruptionRequested do not synchronize with each other
QMutexLocker locker(&d->mutex);
if (!d->running || d->finished || d->isInFinish)
return;
d->interruptionRequested.store(true, std::memory_order_relaxed);
}
请求中断线程。该请求是建议性的,由线程上运行的代码来决定它是否以及如何应此类请求采取行动。如下例子:
while(<condition>) {
if(QThread::currentThread()->isInterruptionRequested())
return;
...
}
4. virtual void run();
void QThread::run()
{
(void) exec();
}
线程的起点。 调用 start() 后,新创建的线程调用此函数。 默认实现只是调用 exec()。
5.void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher);
void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
{
Q_D(QThread);
if (d->data->hasEventDispatcher()) {
qWarning("QThread::setEventDispatcher: An event dispatcher has already been created for this thread");
} else {
eventDispatcher->moveToThread(this);
if (eventDispatcher->thread() == this) // was the move successful?
d->data->eventDispatcher = eventDispatcher;
else
qWarning("QThread::setEventDispatcher: Could not move event dispatcher to target thread");
}
}
将线程的事件分派器设置为eventDispatcher。