@@ -8,6 +8,25 @@ export threadid, nthreads, @threads, @spawn,
88
99Get the ID number of the current thread of execution. The master thread has
1010ID `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"""
1231threadid () = 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
346365the 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.
0 commit comments