Skip to content

Commit 64157d0

Browse files
committed
gh-140374: Add glossary entries related to multithreading
1 parent 237dca5 commit 64157d0

File tree

1 file changed

+174
-8
lines changed

1 file changed

+174
-8
lines changed

Doc/glossary.rst

Lines changed: 174 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ Glossary
134134
iterator's :meth:`~object.__anext__` method until it raises a
135135
:exc:`StopAsyncIteration` exception. Introduced by :pep:`492`.
136136

137+
atomic operation
138+
An operation that completes as a single indivisible unit without
139+
interruption from other threads. Atomic operations are critical for
140+
:term:`thread-safe` programming because they cannot be observed in a
141+
partially completed state by other threads. In the
142+
:term:`free-threaded <free threading>` build, elementary operations
143+
should generally be assumed to be atomic unless the documentation
144+
explicitly states otherwise. See also :term:`race condition` and
145+
:term:`data race`.
146+
137147
attached thread state
138148

139149
A :term:`thread state` that is active for the current OS thread.
@@ -289,6 +299,23 @@ Glossary
289299
advanced mathematical feature. If you're not aware of a need for them,
290300
it's almost certain you can safely ignore them.
291301

302+
concurrency
303+
The ability of different parts of a program to be executed out-of-order
304+
or in partial order without affecting the outcome. This allows for
305+
multiple tasks to make progress during overlapping time periods, though
306+
not necessarily simultaneously. In Python, concurrency can be achieved
307+
through :mod:`threading` (using OS threads), :mod:`asyncio` (cooperative
308+
multitasking), or :mod:`multiprocessing` (separate processes).
309+
See also :term:`parallelism`.
310+
311+
concurrent modification
312+
When multiple threads modify shared data at the same time without
313+
proper synchronization. Concurrent modification can lead to
314+
:term:`data races <data race>` and corrupted data. In the
315+
:term:`free-threaded <free threading>` build, built-in types like
316+
:class:`dict`, :class:`list`, and :class:`set` use internal locks
317+
to protect against concurrent modifications.
318+
292319
context
293320
This term has different meanings depending on where and how it is used.
294321
Some common meanings:
@@ -343,6 +370,16 @@ Glossary
343370
:keyword:`async with` keywords. These were introduced
344371
by :pep:`492`.
345372

373+
critical section
374+
A section of code that accesses shared resources and must not be
375+
executed by multiple threads simultaneously. Critical sections are
376+
typically protected using :term:`locks <lock>` or other
377+
:term:`synchronization primitives <synchronization primitive>` to
378+
ensure :term:`thread-safe` access. In the C API, critical sections
379+
on shared objects can be protected using :c:macro:`Py_BEGIN_CRITICAL_SECTION`
380+
and :c:macro:`Py_END_CRITICAL_SECTION`. See also :term:`lock` and
381+
:term:`race condition`.
382+
346383
CPython
347384
The canonical implementation of the Python programming language, as
348385
distributed on `python.org <https://www.python.org>`_. The term "CPython"
@@ -363,6 +400,24 @@ Glossary
363400
the :term:`cyclic garbage collector <garbage collection>` is to identify these groups and break the reference
364401
cycles so that the memory can be reclaimed.
365402

403+
data race
404+
A situation where multiple threads access the same memory location
405+
concurrently, at least one of the accesses is a write, and the threads
406+
do not use any synchronization to control their access. Data races
407+
lead to :term:`non-deterministic` behavior and can cause data corruption.
408+
Proper use of :term:`locks <lock>` and other :term:`synchronization primitives
409+
<synchronization primitive>` prevents data races. See also
410+
:term:`race condition` and :term:`thread-safe`.
411+
412+
deadlock
413+
A situation where two or more threads are unable to proceed because
414+
each is waiting for the other to release a resource. For example,
415+
if thread A holds lock 1 and waits for lock 2, while thread B holds
416+
lock 2 and waits for lock 1, both threads will wait indefinitely.
417+
Deadlocks can be avoided by always acquiring multiple :term:`locks <lock>`
418+
in a consistent order or by using timeout-based locking. See also
419+
:term:`lock` and :term:`reentrant`.
420+
366421
decorator
367422
A function returning another function, usually applied as a function
368423
transformation using the ``@wrapper`` syntax. Common examples for
@@ -662,6 +717,16 @@ Glossary
662717
requires the GIL to be held in order to use it. This refers to having an
663718
:term:`attached thread state`.
664719

720+
global state
721+
Data that is accessible throughout a program, such as module-level
722+
variables, class variables, or C static variables in :term:`extension modules
723+
<extension module>`. In multi-threaded programs, global state shared
724+
between threads typically requires synchronization to avoid
725+
:term:`race conditions <race condition>`. In the
726+
:term:`free-threaded <free threading>` build, :term:`per-module state`
727+
is often preferred over global state for C extension modules.
728+
See also :term:`per-module state`.
729+
665730
hash-based pyc
666731
A bytecode cache file that uses the hash rather than the last-modified
667732
time of the corresponding source file to determine its validity. See
@@ -706,7 +771,9 @@ Glossary
706771
tuples. Such an object cannot be altered. A new object has to
707772
be created if a different value has to be stored. They play an important
708773
role in places where a constant hash value is needed, for example as a key
709-
in a dictionary.
774+
in a dictionary. Immutable objects are inherently :term:`thread-safe`
775+
because their state cannot be modified after creation, eliminating concerns
776+
about :term:`concurrent modification`.
710777

711778
import path
712779
A list of locations (or :term:`path entries <path entry>`) that are
@@ -796,8 +863,9 @@ Glossary
796863

797864
CPython does not consistently apply the requirement that an iterator
798865
define :meth:`~iterator.__iter__`.
799-
And also please note that the free-threading CPython does not guarantee
800-
the thread-safety of iterator operations.
866+
And also please note that :term:`free-threaded <free threading>`
867+
CPython does not guarantee :term:`thread-safe` behavior of iterator
868+
operations.
801869

802870

803871
key function
@@ -835,10 +903,11 @@ Glossary
835903
:keyword:`if` statements.
836904

837905
In a multi-threaded environment, the LBYL approach can risk introducing a
838-
race condition between "the looking" and "the leaping". For example, the
839-
code, ``if key in mapping: return mapping[key]`` can fail if another
906+
:term:`race condition` between "the looking" and "the leaping". For example,
907+
the code, ``if key in mapping: return mapping[key]`` can fail if another
840908
thread removes *key* from *mapping* after the test, but before the lookup.
841-
This issue can be solved with locks or by using the EAFP approach.
909+
This issue can be solved with :term:`locks <lock>` or by using the
910+
:term:`EAFP` approach. See also :term:`thread-safe`.
842911

843912
lexical analyzer
844913

@@ -857,6 +926,18 @@ Glossary
857926
clause is optional. If omitted, all elements in ``range(256)`` are
858927
processed.
859928

929+
lock
930+
A :term:`synchronization primitive` that allows only one thread at a
931+
time to access a shared resource. A thread must acquire a lock before
932+
accessing the protected resource and release it afterward. If a thread
933+
attempts to acquire a lock that is already held by another thread, it
934+
will block until the lock becomes available. Python's :mod:`threading`
935+
module provides :class:`~threading.Lock` (a basic lock) and
936+
:class:`~threading.RLock` (a :term:`reentrant` lock). Locks are used
937+
to prevent :term:`race conditions <race condition>` and ensure
938+
:term:`thread-safe` access to shared data. See also
939+
:term:`critical section`, :term:`deadlock`, and :term:`reentrant`.
940+
860941
loader
861942
An object that loads a module.
862943
It must define the :meth:`!exec_module` and :meth:`!create_module` methods
@@ -942,8 +1023,11 @@ Glossary
9421023
See :term:`method resolution order`.
9431024

9441025
mutable
945-
Mutable objects can change their value but keep their :func:`id`. See
946-
also :term:`immutable`.
1026+
Mutable objects can change their value but keep their :func:`id`.
1027+
In multi-threaded programs, mutable objects that are shared between
1028+
threads require careful synchronization to avoid :term:`concurrent modification`
1029+
issues. See also :term:`immutable`, :term:`thread-safe`, and
1030+
:term:`concurrent modification`.
9471031

9481032
named tuple
9491033
The term "named tuple" applies to any type or class that inherits from
@@ -1011,6 +1095,16 @@ Glossary
10111095
properties, :meth:`~object.__getattribute__`, class methods, and static
10121096
methods.
10131097

1098+
non-deterministic
1099+
Behavior where the outcome of a program can vary between executions with
1100+
the same inputs. In multi-threaded programs, non-deterministic behavior
1101+
often results from :term:`race conditions <race condition>` where the
1102+
relative timing or interleaving of threads affects the result.
1103+
:term:`Data races <data race>` are a common cause of non-deterministic
1104+
bugs. Proper synchronization using :term:`locks <lock>` and other
1105+
:term:`synchronization primitives <synchronization primitive>` helps
1106+
ensure deterministic behavior.
1107+
10141108
object
10151109
Any data with state (attributes or value) and defined behavior
10161110
(methods). Also the ultimate base class of any :term:`new-style
@@ -1032,6 +1126,15 @@ Glossary
10321126

10331127
See also :term:`regular package` and :term:`namespace package`.
10341128

1129+
parallelism
1130+
The simultaneous execution of multiple operations on different CPU cores.
1131+
True parallelism requires multiple processors or processor cores and
1132+
allows operations to run at exactly the same time, not just interleaved.
1133+
In Python, the :term:`free-threaded <free threading>` build enables
1134+
parallelism for multi-threaded programs by removing the :term:`global
1135+
interpreter lock`. The :mod:`multiprocessing` module also enables
1136+
parallelism by using separate processes. See also :term:`concurrency`.
1137+
10351138
parameter
10361139
A named entity in a :term:`function` (or method) definition that
10371140
specifies an :term:`argument` (or in some cases, arguments) that the
@@ -1116,6 +1219,16 @@ Glossary
11161219
:class:`str` or :class:`bytes` result instead, respectively. Introduced
11171220
by :pep:`519`.
11181221

1222+
per-module state
1223+
State that is stored separately for each instance of a module, rather
1224+
than in :term:`global state`. For :term:`extension modules <extension module>`
1225+
written in C, per-module state helps support multiple interpreters and
1226+
is safer in :term:`free-threaded <free threading>` builds because each
1227+
module instance can have its own data without requiring global
1228+
synchronization. Per-module state is accessed through the module object
1229+
rather than through C static variables. See :ref:`isolating-extensions-howto`
1230+
for more information. See also :term:`global state`.
1231+
11191232
PEP
11201233
Python Enhancement Proposal. A PEP is a design document
11211234
providing information to the Python community, or describing a new
@@ -1206,6 +1319,18 @@ Glossary
12061319
>>> email.mime.text.__name__
12071320
'email.mime.text'
12081321

1322+
race condition
1323+
A flaw in the design or implementation of a program where the correctness
1324+
depends on the relative timing or ordering of events, particularly in
1325+
multi-threaded programs. Race conditions can lead to
1326+
:term:`non-deterministic` behavior and bugs that are difficult to
1327+
reproduce. A :term:`data race` is a specific type of race condition
1328+
involving unsynchronized access to shared memory. The :term:`LBYL`
1329+
coding style is particularly susceptible to race conditions in
1330+
multi-threaded code. Using :term:`locks <lock>` and other
1331+
:term:`synchronization primitives <synchronization primitive>`
1332+
helps prevent race conditions.
1333+
12091334
reference count
12101335
The number of references to an object. When the reference count of an
12111336
object drops to zero, it is deallocated. Some objects are
@@ -1227,6 +1352,23 @@ Glossary
12271352

12281353
See also :term:`namespace package`.
12291354

1355+
reentrant
1356+
A property of a function or :term:`lock` that allows it to be called or
1357+
acquired multiple times by the same thread without causing errors or a
1358+
:term:`deadlock`.
1359+
1360+
For functions, reentrancy means the function can be safely called again
1361+
before a previous invocation has completed, which is important when
1362+
functions may be called recursively or from signal handlers.
1363+
1364+
For locks, Python's :class:`threading.RLock` (reentrant lock) is
1365+
reentrant, meaning a thread that already holds the lock can acquire it
1366+
again without blocking. In contrast, :class:`threading.Lock` is not
1367+
reentrant - attempting to acquire it twice from the same thread will cause
1368+
a deadlock.
1369+
1370+
See also :term:`lock` and :term:`deadlock`.
1371+
12301372
REPL
12311373
An acronym for the "read–eval–print loop", another name for the
12321374
:term:`interactive` interpreter shell.
@@ -1331,6 +1473,17 @@ Glossary
13311473

13321474
See also :term:`borrowed reference`.
13331475

1476+
synchronization primitive
1477+
A basic building block for coordinating the execution of multiple threads
1478+
to ensure :term:`thread-safe` access to shared resources. Python's
1479+
:mod:`threading` module provides several synchronization primitives
1480+
including :class:`~threading.Lock`, :class:`~threading.RLock`,
1481+
:class:`~threading.Semaphore`, :class:`~threading.Condition`,
1482+
:class:`~threading.Event`, and :class:`~threading.Barrier`. These
1483+
primitives help prevent :term:`race conditions <race condition>` and
1484+
coordinate thread execution. See also :term:`lock` and
1485+
:term:`critical section`.
1486+
13341487
t-string
13351488
t-strings
13361489
String literals prefixed with ``t`` or ``T`` are commonly called
@@ -1383,6 +1536,19 @@ Glossary
13831536
See :ref:`Thread State and the Global Interpreter Lock <threads>` for more
13841537
information.
13851538

1539+
thread-safe
1540+
Code that functions correctly when accessed by multiple threads
1541+
concurrently. Thread-safe code uses appropriate
1542+
:term:`synchronization primitives <synchronization primitive>` like
1543+
:term:`locks <lock>` to protect shared mutable state, or is designed
1544+
to avoid shared mutable state entirely. In the
1545+
:term:`free-threaded <free threading>` build, built-in types like
1546+
:class:`dict`, :class:`list`, and :class:`set` use internal locking
1547+
to provide thread-safe operations, though this doesn't guarantee safety
1548+
for all use patterns. Code that is not thread-safe may experience
1549+
:term:`race conditions <race condition>` and :term:`data races <data race>`
1550+
when used in multi-threaded programs.
1551+
13861552
token
13871553

13881554
A small unit of source code, generated by the

0 commit comments

Comments
 (0)