# How do I use an @sync inside a macro

**URL:** <https://discourse.julialang.org/t/how-do-i-use-an-sync-inside-a-macro/80023>\
**Category:** General Usage\
**Tags:** macros, multithreading\
**Created:** [April 26, 2022, 12:40am UTC](https://discourse.julialang.org/t/how-do-i-use-an-sync-inside-a-macro/80023 "2022-04-26T00:40:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [April 26, 2022, 12:40am UTC](https://discourse.julialang.org/t/how-do-i-use-an-sync-inside-a-macro/80023/1 "2022-04-26T00:40:10Z")

</div>

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?

```julia
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()

```
