我是靠谱客的博主 愉快菠萝,这篇文章主要介绍Qt源码分析--QThread(3),现在分享给大家,希望可以做个参考。

1.void exit(int retcode = 0);

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;

复制代码
1
2
3
4
5
6
7
bool QThread::isFinished() const { Q_D(const QThread); QMutexLocker locker(&d->mutex); return d->finished || d->isInFinish; }

4.bool isInterruptionRequested() const;

复制代码
1
2
3
4
5
6
7
8
9
10
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;

复制代码
1
2
3
4
5
6
7
bool QThread::isRunning() const { Q_D(const QThread); QMutexLocker locker(&d->mutex); return d->running && !d->isInFinish; }

最后

以上就是愉快菠萝最近收集整理的关于Qt源码分析--QThread(3)的全部内容,更多相关Qt源码分析--QThread(3)内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部