# Multithreading a over a double for-loop with arrays

**URL:** <https://discourse.julialang.org/t/multithreading-a-over-a-double-for-loop-with-arrays/130033>\
**Category:** Performance\
**Tags:** parallel, multithreading\
**Created:** [June 19, 2025, 4:34pm UTC](https://discourse.julialang.org/t/multithreading-a-over-a-double-for-loop-with-arrays/130033 "2025-06-19T16:34:44Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Menno](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Menno](https://discourse.julialang.org/u/Menno)\
**Post date:** [June 19, 2025, 9:22pm UTC](https://discourse.julialang.org/t/multithreading-a-over-a-double-for-loop-with-arrays/130033/4 "2025-06-19T21:22:14Z")

</div>

You are right, that makes it more conceptually difficult. The reason that I coded it this way is that the loops are both sometimes small, and run over a range that is not divisible by the number of threads. If it works properly, this should work faster without needing to construct a complicated iterable. The reason I thought it would work is because of [Parallelize nested loop in v1.72](https://discourse.julialang.org/t/parallelize-nested-loop-in-v1-72/85612). Just from trying printing threadid, mu and i anside of each line, it seems to divide the different combinations of parameters up OK.  
Anyway, I have the same problem when parallellizing one loop, which I of course checked.  
Also, all the MWE’s I have tried to build up until now with a similar structure have worked. My question is basically just where to look for a race condition in my large chunk of code. Could that occur, for example, if I have a global variable N which is constant but is used in the code? And could it happen if I pass the same parameter p to run\_test in each loop? My guess is yes, but I would expect that a) they are rare, and b) julia has easy ways to program something to prevent this from happening, which I could sadly not find in the documentation. It seems to me that the compiler could also be clever enough to avoid these types of race conditions, but I just don’t know in which cases it does. I should have focused my question more in that direction.

---

_[View the full topic](https://discourse.julialang.org/t/multithreading-a-over-a-double-for-loop-with-arrays/130033)._
