Skip to content

Commit b63ba85

Browse files
committed
add experimental @par macro to Base.Threads
1 parent f578ada commit b63ba85

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

base/threadingconstructs.jl

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
export threadid, nthreads, @threads
3+
export threadid, nthreads, @threads, @par
44

55
"""
66
Threads.threadid()
@@ -102,3 +102,23 @@ macro threads(args...)
102102
throw(ArgumentError("unrecognized argument to @threads"))
103103
end
104104
end
105+
106+
"""
107+
Threads.@par 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+
!!! compat "Julia 1.3"
114+
This macro is available as of Julia 1.3.
115+
"""
116+
macro par(expr)
117+
thunk = esc(:(()->($expr)))
118+
quote
119+
local task = Task($thunk)
120+
task.sticky = false
121+
schedule(task)
122+
task
123+
end
124+
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.@par
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 = @par pfib(n-2)
653651
return pfib(n-1) + fetch(t)::Int
654652
end
655653
@test pfib(20) == 6765

0 commit comments

Comments
 (0)