Behavior of `Threads.@threads for` loop

I strongly suggest avoiding reasoning about worker (OS) threads in Julia programs. It is a very low-level construct and arguably it should be avoided in idiomatic Julia programs. The primitive for concurrent execution in Julia is Task and not thread. Basically, if you write threadid() or nthreads() anywhere, that’s a big warning sign unless you know what you are doing. (Note: I already have mentioned the exceptions which I consider is fine. I’m not repeating them and my comment here only discusses what should be done in principle.)

There are various ways to specify “local” resources. For example, FLoops.jl provides @init macro to declare basecase-local (approximately task-local) resource. See also Concurrency patterns for controlled parallelisms for various composable idioms for handling local resources.