Skip to content

Commit 5f873fd

Browse files
IanButterworthKristofferC
authored andcommitted
add docs on task migration (#50047)
(cherry picked from commit ff23b37)
1 parent 258aeff commit 5f873fd

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

base/threadingconstructs.jl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ export threadid, nthreads, @threads, @spawn,
88
99
Get the ID number of the current thread of execution. The master thread has
1010
ID `1`.
11+
12+
# Examples
13+
```julia-repl
14+
julia> Threads.threadid()
15+
1
16+
17+
julia> Threads.@threads for i in 1:4
18+
println(Threads.threadid())
19+
end
20+
4
21+
2
22+
5
23+
4
24+
```
25+
26+
!!! note
27+
The thread that a task runs on may change if the task yields, which is known as [`Task Migration`](@ref man-task-migration).
28+
For this reason in most cases it is not safe to use `threadid()` to index into, say, a vector of buffer or stateful objects.
29+
1130
"""
1231
threadid() = Int(ccall(:jl_threadid, Int16, ())+1)
1332

@@ -220,7 +239,7 @@ For example, the above conditions imply that:
220239
- Write only to locations not shared across iterations (unless a lock or atomic operation is
221240
used).
222241
- The value of [`threadid()`](@ref Threads.threadid) may change even within a single
223-
iteration.
242+
iteration. See [`Task Migration`](@ref man-task-migration)
224243
225244
## Schedulers
226245
@@ -346,8 +365,10 @@ the _value_ of a variable, isolating the asynchronous code from changes to
346365
the variable's value in the current task.
347366
348367
!!! note
349-
See the manual chapter on [multi-threading](@ref man-multithreading)
350-
for important caveats. See also the chapter on [threadpools](@ref man-threadpools).
368+
The thread that the task runs on may change if the task yields, therefore `threadid()` should not
369+
be treated as constant for a task. See [`Task Migration`](@ref man-task-migration), and the broader
370+
[multi-threading](@ref man-multithreading) manual for further important caveats.
371+
See also the chapter on [threadpools](@ref man-threadpools).
351372
352373
!!! compat "Julia 1.3"
353374
This macro is available as of Julia 1.3.

doc/src/manual/multi-threading.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ threads in Julia:
372372
This may require some transitional work across the ecosystem before threading
373373
can be widely adopted with confidence. See the next section for further details.
374374

375+
## [Task Migration](@id man-task-migration)
376+
377+
After a task starts running on a certain thread (e.g. via [`@spawn`](@ref Threads.@spawn) or
378+
[`@threads`](@ref Threads.@threads)), it may move to a different thread if the task yields.
379+
380+
This means that [`threadid()`](@ref Threads.threadid) should not be treated as constant within a task, and therefore
381+
should not be used to index into a vector of buffers or stateful objects.
382+
383+
!!! compat "Julia 1.7"
384+
Task migration was introduced in Julia 1.7. Before this tasks always remained on the same thread that they were
385+
started on.
386+
375387
## Safe use of Finalizers
376388

377389
Because finalizers can interrupt any code, they must be very careful in how

0 commit comments

Comments
 (0)