@threads and two functions calls inside a loop

Hi,

I have this use case:

@threads loop
    function(parameters_1)
    function(parameters_2)
end

I would like to maximize load, problem is that sometimes everything is finished but the last iteration of the loop, meaning function(parameters_2) has to wait for function(parameters_1) to finish.
I don’t know how to handle that.

You could use @spawn, see Announcing composable multi-threaded parallelism in Julia or the official docs.

2 Likes

Thanks.

I ended up with:

using Base.Threads

@threads for elem in ["A", "B"]
    for i in 1:1:10
        x=@spawn println(elem * "1")
        y=@spawn println(elem * "2")
        wait(x)
        wait(y)
    end
end

Output:

B1
A1
A2
B2
A2
A1
B2
A1
A2
B1
A2
B1
B2
B2
A1
A2
A1
B1
A2
B1
A1
B2
A1
B2
A2
B1
A1
A2
B2
A1
A2
A1
B1
B1
B2
B1
B2
B2
B1
A2

Looks like it’s working.

Mmm, sometimes with the real big processing I got:

nested task error: UndefVarError: `x` not defined