Skip to content

Commit f4d1381

Browse files
authored
Document Threads.threadid(::Task) (#55369)
This is quite handy to figure out which thread a task is running on, and I couldn't find another way to do it from outside the task.
1 parent 065d456 commit f4d1381

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

base/threadingconstructs.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ export threadid, nthreads, @threads, @spawn,
44
threadpool, nthreadpools
55

66
"""
7-
Threads.threadid() -> Int
7+
Threads.threadid([t::Task]) -> Int
88
9-
Get the ID number of the current thread of execution. The master thread has
10-
ID `1`.
9+
Get the ID number of the current thread of execution, or the thread of task
10+
`t`. The master thread has ID `1`.
1111
1212
# Examples
1313
```julia-repl
@@ -21,12 +21,15 @@ julia> Threads.@threads for i in 1:4
2121
2
2222
5
2323
4
24+
25+
julia> Threads.threadid(Threads.@spawn "foo")
26+
2
2427
```
2528
2629
!!! note
2730
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-
31+
For this reason in most cases it is not safe to use `threadid([task])` to index into, say, a vector of buffers or stateful
32+
objects.
3033
"""
3134
threadid() = Int(ccall(:jl_threadid, Int16, ())+1)
3235

0 commit comments

Comments
 (0)