Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qyieldcpu.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2023 The Qt Company Ltd.
2
// Copyright (C) 2023 Intel Corporation.
3
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
4
5
/*!
6
\fn qYieldCpu()
7
\inmodule QtCore
8
\ingroup thread
9
\relates QAtomicInteger
10
//! \relatesalso QAtomicPointer
11
\since 6.7
12
13
Pauses the execution of the current thread for an unspecified time, using
14
hardware instructions, without de-scheduling this thread. This function is
15
meant to be used in high-throughput loops where the code expects another
16
thread to modify an atomic variable. This is completely different from
17
QThread::yieldCurrentThread(), which is an OS-level operation that may take
18
the whole thread off the CPU and allow other threads (possibly belonging to
19
other processes) to run.
20
21
So, instead of
22
\code
23
while (!condition)
24
;
25
\endcode
26
27
one should write
28
\code
29
while (!condition)
30
qYieldCpu();
31
\endcode
32
33
This is useful both with and without hardware multithreading on the same
34
core. In the case of hardware threads, it serves to prevent further
35
speculative execution filling up the pipeline, which could starve the
36
sibling thread of resources. Across cores and higher levels of separation,
37
it allows the cache coherency protocol to allocate the cache line being
38
modified and inspected to the logical processor whose result this code is
39
expecting.
40
41
It is also recommended to loop around code that does not modify the global
42
variable, to avoid contention in exclusively obtaining the memory location.
43
Therefore, an atomic modification loop such as a spinlock acquisition
44
should be:
45
46
\code
47
while (true) {
48
while (!readOnlyCondition(atomic))
49
qYieldCpu();
50
if (modify(atomic))
51
break;
52
}
53
\endcode
54
55
On x86 processors and on RISC-V processors with the \c{Zihintpause}
56
extension, this will emit the \c PAUSE instruction, which is ignored on
57
processors that don't support it; on ARMv7 or later ARM processors, it will
58
emit the \c{YIELD} instruction.
59
*/
qtbase
src
corelib
thread
qyieldcpu.qdoc
Generated on Mon Mar 10 2025 01:10:22 for Qt by
1.13.2