Oof, I apologize. I have this bad habit of seeing something and thinking “oh, I can be helpful with that”, but then ultimately reading carelessly enough that I miss something obvious.
In any case, having actually read that above code more carefully, here’s an example analog that does this with multiple threads:
using ThreadPools
myarr = [1:5 for i in 1:5] # Generate for Iterators.Product example
### Start multi-threading to fill an array (a simple example) ###
numElements = prod(length(i) for i in myarr)
const x = zeros(numElements)
# I want to split the below for loop among the available threads
iter = enumerate(Iterators.product((1:length(j) for j in myarr)...))
ThreadPools.tforeach(iter) do xi
(i, cartProd) = xi # de-structure x, this is probably possible in the above line.
x[i] = Threads.threadid()
end
This uses ThreadPools.jl, which is yet another option, which I picked because it provides a threaded foreach that will look the most like your loop. Even though we didn’t split the thing in half by length manually and didn’t collect, you can look at how many entries of x take the value of each threadid and see that ThreadPools does that kind of work for you. Like I say in the comments, there’s probably a way to destructure xi in the same line as the do block, but hopefully the point is now clear. This answers your question, right? Sorry again for earlier.