Skip to content

Commit ff23b37

Browse files
add docs on task migration (#50047)
1 parent 0c774c7 commit ff23b37

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

base/threadingconstructs.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ julia> Threads.@threads for i in 1:4
2222
5
2323
4
2424
```
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+
2530
"""
2631
threadid() = Int(ccall(:jl_threadid, Int16, ())+1)
2732

@@ -229,7 +234,7 @@ For example, the above conditions imply that:
229234
- Write only to locations not shared across iterations (unless a lock or atomic operation is
230235
used).
231236
- The value of [`threadid()`](@ref Threads.threadid) may change even within a single
232-
iteration.
237+
iteration. See [`Task Migration`](@ref man-task-migration)
233238
234239
## Schedulers
235240
@@ -355,8 +360,10 @@ the _value_ of a variable, isolating the asynchronous code from changes to
355360
the variable's value in the current task.
356361
357362
!!! note
358-
See the manual chapter on [multi-threading](@ref man-multithreading)
359-
for important caveats. See also the chapter on [threadpools](@ref man-threadpools).
363+
The thread that the task runs on may change if the task yields, therefore `threadid()` should not
364+
be treated as constant for a task. See [`Task Migration`](@ref man-task-migration), and the broader
365+
[multi-threading](@ref man-multithreading) manual for further important caveats.
366+
See also the chapter on [threadpools](@ref man-threadpools).
360367
361368
!!! compat "Julia 1.3"
362369
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
@@ -388,6 +388,18 @@ threads in Julia:
388388
This may require some transitional work across the ecosystem before threading
389389
can be widely adopted with confidence. See the next section for further details.
390390

391+
## [Task Migration](@id man-task-migration)
392+
393+
After a task starts running on a certain thread (e.g. via [`@spawn`](@ref Threads.@spawn) or
394+
[`@threads`](@ref Threads.@threads)), it may move to a different thread if the task yields.
395+
396+
This means that [`threadid()`](@ref Threads.threadid) should not be treated as constant within a task, and therefore
397+
should not be used to index into a vector of buffers or stateful objects.
398+
399+
!!! compat "Julia 1.7"
400+
Task migration was introduced in Julia 1.7. Before this tasks always remained on the same thread that they were
401+
started on.
402+
391403
## Safe use of Finalizers
392404

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

0 commit comments

Comments
 (0)