@@ -22,6 +22,11 @@ julia> Threads.@threads for i in 1:4
22225
23234
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"""
2631threadid () = 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
355360the 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.
0 commit comments