Skip to content

Commit c86700d

Browse files
authored
add experimental @spawn macro to Base.Threads (#32600)
1 parent 684973e commit c86700d

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

NEWS.md

+27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Multi-threading changes
1919
although it specifically does include `BufferStream`.
2020
([#32309], [#32174], [#31981], [#32421]).
2121
* The global random number generator (`GLOBAL_RNG`) is now thread-safe (and thread-local) ([#32407]).
22+
* New experimental `Threads.@spawn` macro that runs a task on any available thread ([#32600]).
2223

2324
Build system changes
2425
--------------------
@@ -104,3 +105,29 @@ Tooling Improvements
104105
may be run using `make -C src analyzegc`.
105106

106107
<!--- generated by NEWS-update.jl: -->
108+
[#23422]: https://github.com/JuliaLang/julia/issues/23422
109+
[#24353]: https://github.com/JuliaLang/julia/issues/24353
110+
[#31066]: https://github.com/JuliaLang/julia/issues/31066
111+
[#31459]: https://github.com/JuliaLang/julia/issues/31459
112+
[#31576]: https://github.com/JuliaLang/julia/issues/31576
113+
[#31654]: https://github.com/JuliaLang/julia/issues/31654
114+
[#31664]: https://github.com/JuliaLang/julia/issues/31664
115+
[#31781]: https://github.com/JuliaLang/julia/issues/31781
116+
[#31834]: https://github.com/JuliaLang/julia/issues/31834
117+
[#31838]: https://github.com/JuliaLang/julia/issues/31838
118+
[#31853]: https://github.com/JuliaLang/julia/issues/31853
119+
[#31916]: https://github.com/JuliaLang/julia/issues/31916
120+
[#31981]: https://github.com/JuliaLang/julia/issues/31981
121+
[#32002]: https://github.com/JuliaLang/julia/issues/32002
122+
[#32103]: https://github.com/JuliaLang/julia/issues/32103
123+
[#32122]: https://github.com/JuliaLang/julia/issues/32122
124+
[#32133]: https://github.com/JuliaLang/julia/issues/32133
125+
[#32174]: https://github.com/JuliaLang/julia/issues/32174
126+
[#32260]: https://github.com/JuliaLang/julia/issues/32260
127+
[#32300]: https://github.com/JuliaLang/julia/issues/32300
128+
[#32308]: https://github.com/JuliaLang/julia/issues/32308
129+
[#32309]: https://github.com/JuliaLang/julia/issues/32309
130+
[#32407]: https://github.com/JuliaLang/julia/issues/32407
131+
[#32421]: https://github.com/JuliaLang/julia/issues/32421
132+
[#32534]: https://github.com/JuliaLang/julia/issues/32534
133+
[#32600]: https://github.com/JuliaLang/julia/issues/32600

base/threadingconstructs.jl

+27
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,30 @@ macro threads(args...)
102102
throw(ArgumentError("unrecognized argument to @threads"))
103103
end
104104
end
105+
106+
"""
107+
Threads.@spawn expr
108+
109+
Create and run a [`Task`](@ref) on any available thread. To wait for the task to
110+
finish, call [`wait`](@ref) on the result of this macro, or call [`fetch`](@ref)
111+
to wait and then obtain its return value.
112+
113+
!!! note
114+
This feature is currently considered experimental.
115+
116+
!!! compat "Julia 1.3"
117+
This macro is available as of Julia 1.3.
118+
"""
119+
macro spawn(expr)
120+
thunk = esc(:(()->($expr)))
121+
var = esc(Base.sync_varname)
122+
quote
123+
local task = Task($thunk)
124+
task.sticky = false
125+
if $(Expr(:isdefined, var))
126+
push!($var, task)
127+
end
128+
schedule(task)
129+
task
130+
end
131+
end

doc/src/base/multi-threading.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ described here might (and likely will) change in the future.
77
Base.Threads.threadid
88
Base.Threads.nthreads
99
Base.Threads.@threads
10+
Base.Threads.@spawn
1011
```
1112

1213
```@docs

test/threads_exec.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,7 @@ function pfib(n::Int)
647647
if n <= 1
648648
return n
649649
end
650-
t = @task pfib(n-2)
651-
t.sticky = false
652-
schedule(t)
650+
t = Threads.@spawn pfib(n-2)
653651
return pfib(n-1) + fetch(t)::Int
654652
end
655653
@test pfib(20) == 6765

0 commit comments

Comments
 (0)