我是靠谱客的博主 愉快菠萝,最近开发中收集的这篇文章主要介绍Qt源码分析--QThread(3),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.void exit(int retcode = 0);

void QThread::exit(int returnCode)
{
    Q_D(QThread);
    QMutexLocker locker(&d->mutex);
    d->exited = true;
    d->returnCode = returnCode;
    d->data->quitNow = true;
    for (int i = 0; i < d->data->eventLoops.size(); ++i) {
        QEventLoop *eventLoop = d->data->eventLoops.at(i);
        eventLoop->exit(returnCode);
    }
}

void QEventLoop::exit(int returnCode)
{
    Q_D(QEventLoop);
    if (!d->threadData->hasEventDispatcher())
        return;
    d->returnCode.storeRelaxed(returnCode);
    d->exit.storeRelease(true);
    d->threadData->eventDispatcher.loadRelaxed()->interrupt();
#ifdef Q_OS_WASM
    // QEventLoop::exec() never returns in emscripten. We implement approximate behavior here.
    // QTBUG-70185
    if (d->threadData->loopLevel == 1) {
        emscripten_force_exit(returnCode);
    } else {
        d->inExec = false;
        --d->threadData->loopLevel;
    }
#endif
}

调用此函数后,线程离开事件循环并从对 QEventLoop::exec() 的调用返回。

2.static int idealThreadCount() noexcept;

int QThread::idealThreadCount() noexcept
{
    int cores = 1;
#if defined(Q_OS_HPUX)
    // HP-UX
    struct pst_dynamic psd;
    if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) == -1) {
        perror("pstat_getdynamic");
    } else {
        cores = (int)psd.psd_proc_cnt;
    }
#elif defined(Q_OS_BSD4)
    // FreeBSD, OpenBSD, NetBSD, BSD/OS, OS X, iOS
    size_t len = sizeof(cores);
    int mib[2];
    mib[0] = CTL_HW;
    mib[1] = HW_NCPU;
    if (sysctl(mib, 2, &cores, &len, NULL, 0) != 0) {
        perror("sysctl");
    }
#elif defined(Q_OS_INTEGRITY)
#if (__INTEGRITY_MAJOR_VERSION >= 10)
    // Integrity V10+ does support multicore CPUs
    Value processorCount;
    if (GetProcessorCount(CurrentTask(), &processorCount) == 0)
        cores = processorCount;
    else
#endif
    // as of aug 2008 Integrity only supports one single core CPU
    cores = 1;
#elif defined(Q_OS_VXWORKS)
    // VxWorks
#  if defined(QT_VXWORKS_HAS_CPUSET)
    cpuset_t cpus = vxCpuEnabledGet();
    cores = 0;
    // 128 cores should be enough for everyone ;)
    for (int i = 0; i < 128 && !CPUSET_ISZERO(cpus); ++i) {
        if (CPUSET_ISSET(cpus, i)) {
            CPUSET_CLR(cpus, i);
            cores++;
        }
    }
#  else
    // as of aug 2008 VxWorks < 6.6 only supports one single core CPU
    cores = 1;
#  endif
#elif defined(Q_OS_WASM)
    cores = QThreadPrivate::idealThreadCount;
#else
    // the rest: Linux, Solaris, AIX, Tru64
    cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
    if (cores == -1)
        return 1;
#endif
    return cores;
}

返回可以在系统上运行的理想线程数。

3.bool isFinished() const;

bool QThread::isFinished() const
{
    Q_D(const QThread);
    QMutexLocker locker(&d->mutex);
    return d->finished || d->isInFinish;
}

4.bool isInterruptionRequested() const;

bool QThread::isInterruptionRequested() const
{
    Q_D(const QThread);
    // fast path: check that the flag is not set:
    if (!d->interruptionRequested.load(std::memory_order_relaxed))
        return false;
    // slow path: if the flag is set, take into account run status:
    QMutexLocker locker(&d->mutex);
    return d->running && !d->finished && !d->isInFinish;
}

如果应该停止在此线程上运行的任务,则返回 true。 用于长时间跑的任务。

5.bool isRunning() const;

bool QThread::isRunning() const
{
    Q_D(const QThread);
    QMutexLocker locker(&d->mutex);
    return d->running && !d->isInFinish;
}

最后

以上就是愉快菠萝为你收集整理的Qt源码分析--QThread(3)的全部内容,希望文章能够帮你解决Qt源码分析--QThread(3)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部