I have some code which looks like this:
Threads.@threads for branch in tree Threads.@threads for twig in branch append!(biglist, dostuff.(twig)) end end
tree is a vector (tree) of vectors (branch) of vectors (twigs) of floats (leaves).
Obviously this isn’t thread safe, so I tried to make it threadsafe like this:
function findlength(tree) for branch in tree for twig in branch biglistlength += length(twig) end end return biglistlength end biglist = zeros(findlength(tree)) j = 0 Threads.@threads for branch in tree Threads.@threads for twig in branch for (i,leaf) in enumerate(twig) biglist[i+j] = dostuff(leaf) end j += length(twig) end end
But this isn’t thread safe either because there is a race condition in the
j += step.
What’s the correct way of going about something like this? I looked at the threading documentation and the atomic stuff, the locking stuff, as well as the floop stuff all look to be very complicated and would almost work here. The atomic and locking seem to devolve the two loops into a serial operation which defeats the purpose of making it threaded, while the floop reduction seems to almost work except I still need to figure out the indices in my list somehow.
Edit: I know someone will tell me that nested vectors isn’t the way to go, but my real code doesn’t actually look like this, and this kind of construction is definitely what I need.