Combining @sync and @async with for and while loops

I am relatively new to Julia, and I am trying to understand the @sync and @async macro, and how they act when combined with for and while loops.

I wonder if someone could tell me what the difference between these two are, if any?

function do_calculations(e)
    # Simulate time it takes to do some calculation
    sleep(e.time_to_do_calculations)
end

# Alternative 1
@sync begin
    for e in lst
        @async begin
            while running
                do_calculations(e)
            end
        end
    end
end

# Alternative 2
@sync for e in lst
    @async while running
        do_calculations(e)
    end
end

in lst there is elements that we want to do calculations on using do_calculations(e), where each calculation takes some time. However they take different times depending on the element e. But I want each of the calculations on the different elements to run independently from each other, thus the use of @async. running is used to define how long these calculations should be running for.

Thanks in advance!

1 Like