@@ -22,6 +22,11 @@ julia> Threads.@threads for i in 1:4
22
22
5
23
23
4
24
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
+
25
30
"""
26
31
threadid () = Int (ccall (:jl_threadid , Int16, ())+ 1 )
27
32
@@ -229,7 +234,7 @@ For example, the above conditions imply that:
229
234
- Write only to locations not shared across iterations (unless a lock or atomic operation is
230
235
used).
231
236
- The value of [`threadid()`](@ref Threads.threadid) may change even within a single
232
- iteration.
237
+ iteration. See [`Task Migration`](@ref man-task-migration)
233
238
234
239
## Schedulers
235
240
@@ -355,8 +360,10 @@ the _value_ of a variable, isolating the asynchronous code from changes to
355
360
the variable's value in the current task.
356
361
357
362
!!! 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).
360
367
361
368
!!! compat "Julia 1.3"
362
369
This macro is available as of Julia 1.3.
0 commit comments