Behavior of `Threads.@threads for` loop

No, @threads currently spawns only nthreads() tasks, and each task processes a contiguous region of the input. In the current implementation, the task tree structure of :dynamic and :static is identital. The only difference is that the underlying tasks in :dynamic can be scheduled on an arbitrary worker thread and is allowed to migrate across worker threads, like the usual tasks created via Threads.@spawn.

The details may change in the future but I believe the number of tasks will always be O(nthreads()) and each task process one contiguous region (or a few contiguous regions). I’m almost certain this will always be the case since otherwise, it’d be essentially equivalent to @sync for x in xs; @spawn ...; end.

(This is not apparent from the current docstring Multi-Threading · The Julia Language and so we probably should add some more clarifications.)

1 Like