Repeated multithreaded region

I’m wondering what the best way to repeatedly call a parallelized piece of code in a serial loop? For example, in smooth nonlinear optimization, we often need to calculate an expansion of objective and constraints about the current iterate, which often can be done in parallel, e.g.

for iter = 1:max_iters
    @threads for i = 1:nwork
        dowork(i)
    end
    computestep()
    updateiterate()
end

How do we avoid the overhead of setting up threads each time through the loop? In a paradigm such as OpenMP, you’d set up the threads once, split the work among works for the parallel section, and assign the serial portion to the main thread, with the other threads idling. What’s the “right” way to do this in Julia? Is @spawn a better option here? The few times I’ve tried doing stuff like this the overhead of calling @threads has made it much slower than a serial version of the code.

Without knowing a better way to create a threadpool, I would spawn a bunch of channels and have them take as a “do-work-now” token

You might want to try @batch from Polyester.jl (as a direct replacement for @threads) . It should have lower overhead because (AFAIU) it “reuses” threads more efficiently.