I’d like to understand how I could use @sync inside a macro instead of using fetch
. Below is a simple example with some code borrowed from Base src (and butchered). How would I do it?
function yuck( iter, lbody, n )
lidx = iter.args[1]
range = iter.args[2]
return quote
let range = $(esc(range))
n = $(esc(n))
lenr = length(range)
len = div( lenr, n )
rv = Vector{Task}(undef, n)
for yucky ∈ 1:n
rv[yucky] = Threads.@spawn begin
f = firstindex( range ) + ((yucky-1) * len)
ℓ = f + len - 1
for i ∈ f : ℓ
local $(esc(lidx)) = @inbounds range[i]
$(esc(lbody))
sleep(0.5)
end
end
end
for yucky ∈ 1:n
fetch( rv[yucky] )
end
end
end
end
macro yuck( n :: Int, ex )
if n == 1
return quote
$(esc(ex))
end
end
yuck( ex.args[1], ex.args[2], n )
end
function ugh( )
@yuck 10 for i ∈ 1:100
println( i )
end
end
ugh()