Skip to content

Commit 872e3f2

Browse files
add option to specify :same threadpool on spawn
1 parent 2c3b5a8 commit 872e3f2

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ New language features
2323
- actual running time for the task (`Base.Experimental.task_running_time_ns`), and
2424
- wall-time for the task (`Base.Experimental.task_wall_time_ns`).
2525
- Support for Unicode 16 ([#56925]).
26+
- `Threads.@spawn` now takes a `:same` argument to specify the same threadpool as the caller.
27+
`Threads.@spawn :same foo()` which is shorthand for `Threads.@spawn Threads.threadpool() foo()` ([#])
2628

2729
Language changes
2830
----------------

base/threadingconstructs.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,11 @@ function _spawn_set_thrpool(t::Task, tp::Symbol)
440440
end
441441

442442
"""
443-
Threads.@spawn [:default|:interactive] expr
443+
Threads.@spawn [:default|:interactive|:same] expr
444444
445445
Create a [`Task`](@ref) and [`schedule`](@ref) it to run on any available
446-
thread in the specified threadpool (`:default` if unspecified). The task is
446+
thread in the specified threadpool: `:default`, `:interactive`, or `:same`
447+
to use the same as the caller. `:default` is used if unspecified. The task is
447448
allocated to a thread once one becomes available. To wait for the task to
448449
finish, call [`wait`](@ref) on the result of this macro, or call
449450
[`fetch`](@ref) to wait and then obtain its return value.
@@ -468,6 +469,9 @@ the variable's value in the current task.
468469
!!! compat "Julia 1.9"
469470
A threadpool may be specified as of Julia 1.9.
470471
472+
!!! compat "Julia 1.12"
473+
The same threadpool may be specified as of Julia 1.12.
474+
471475
# Examples
472476
```julia-repl
473477
julia> t() = println("Hello from ", Threads.threadid());

test/threadpool_use.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ using Base.Threads
99
@test fetch(Threads.@spawn Threads.threadpool()) === :default
1010
@test fetch(Threads.@spawn :default Threads.threadpool()) === :default
1111
@test fetch(Threads.@spawn :interactive Threads.threadpool()) === :interactive
12+
@test fetch(Threads.@spawn :same Threads.threadpool()) === Threads.threadpool()
13+
@sync for tp in [:interactive, :default]
14+
Threads.@spawn tp begin
15+
@test fetch(Threads.@spawn :same Threads.threadpool()) === Threads.threadpool()
16+
end
17+
end
18+
wait(Threads.@spawn :interactive begin
19+
@test fetch(Threads.@spawn :same Threads.threadpool()) === Threads.threadpool()
20+
end)
1221
tp = :default
1322
@test fetch(Threads.@spawn tp Threads.threadpool()) === :default
1423
tp = :interactive

0 commit comments

Comments
 (0)