Nested parallel loops with @spawn

I think solve_model_spawn2! is fast because it does not wait until the inner tasks are finished. See:

julia> t1 = Threads.@spawn begin
           global t2
           t2 = Threads.@spawn begin
               sleep(1.0)
               println("DONE")
           end
       end
       wait(t1) # this does not wait t2
       t2
Task (runnable) @0x00007f0eaff61ae0

julia> wait(t2)
DONE

As you can see, using raw-@spawn directly is pretty dangerous, since it does not do structured concurrency. Using @threads is OK but I think using map/filter/reduce framework for multithreading is a better option (e.g., KissThreading.jl, Transducers.jl (that’s my library)).

1 Like