Skip to content

Commit

Permalink
Document Threads.threadid(::Task) (#55369)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JamesWrigley committed Aug 4, 2024
1 parent 065d456 commit f4d1381
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions base/threadingconstructs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export threadid, nthreads, @threads, @spawn,
threadpool, nthreadpools

"""
Threads.threadid() -> Int
Threads.threadid([t::Task]) -> Int
Get the ID number of the current thread of execution. The master thread has
ID `1`.
Get the ID number of the current thread of execution, or the thread of task
`t`. The master thread has ID `1`.
# Examples
```julia-repl
Expand All @@ -21,12 +21,15 @@ julia> Threads.@threads for i in 1:4
2
5
4
julia> Threads.threadid(Threads.@spawn "foo")
2
```
!!! note
The thread that a task runs on may change if the task yields, which is known as [`Task Migration`](@ref man-task-migration).
For this reason in most cases it is not safe to use `threadid()` to index into, say, a vector of buffer or stateful objects.
For this reason in most cases it is not safe to use `threadid([task])` to index into, say, a vector of buffers or stateful
objects.
"""
threadid() = Int(ccall(:jl_threadid, Int16, ())+1)

Expand Down

0 comments on commit f4d1381

Please sign in to comment.