@@ -440,10 +440,11 @@ function _spawn_set_thrpool(t::Task, tp::Symbol)
440
440
end
441
441
442
442
"""
443
- Threads.@spawn [:default|:interactive] expr
443
+ Threads.@spawn [:default|:interactive|:samepool ] expr
444
444
445
445
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 `:samepool`
447
+ to use the same as the caller. `:default` is used if unspecified. The task is
447
448
allocated to a thread once one becomes available. To wait for the task to
448
449
finish, call [`wait`](@ref) on the result of this macro, or call
449
450
[`fetch`](@ref) to wait and then obtain its return value.
@@ -468,6 +469,9 @@ the variable's value in the current task.
468
469
!!! compat "Julia 1.9"
469
470
A threadpool may be specified as of Julia 1.9.
470
471
472
+ !!! compat "Julia 1.12"
473
+ The same threadpool may be specified as of Julia 1.12.
474
+
471
475
# Examples
472
476
```julia-repl
473
477
julia> t() = println("Hello from ", Threads.threadid());
@@ -486,7 +490,7 @@ macro spawn(args...)
486
490
ttype, ex = args
487
491
if ttype isa QuoteNode
488
492
ttype = ttype. value
489
- if ttype != = :interactive && ttype != = :default
493
+ if ! in ( ttype, ( :interactive , :default , :samepool ))
490
494
throw (ArgumentError (LazyString (" unsupported threadpool in @spawn: " , ttype)))
491
495
end
492
496
tp = QuoteNode (ttype)
@@ -507,7 +511,11 @@ macro spawn(args...)
507
511
let $ (letargs... )
508
512
local task = Task ($ thunk)
509
513
task. sticky = false
510
- _spawn_set_thrpool (task, $ (esc (tp)))
514
+ local tp = $ (esc (tp))
515
+ if tp == :samepool
516
+ tp = Threads. threadpool ()
517
+ end
518
+ _spawn_set_thrpool (task, tp)
511
519
if $ (Expr (:islocal , var))
512
520
put! ($ var, task)
513
521
end
0 commit comments