# Using @distributed for loop

**URL:** https://discourse.julialang.org/t/using-distributed-for-loop/117831
**Category:** New to Julia
**Tags:** question, loops, parallel-computing
**Created:** [August 5, 2024, 8:08am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831 "2024-08-05T08:08:51Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [August 5, 2024, 8:20am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/3 "2024-08-05T08:20:15Z")

</div>

Hi, your problem is very similar to another recent thread where I explained the issue:

> [@Probable data race condition causing problems when trying to parallelize a loop used to populate an array](https://discourse.julialang.org/t/probable-data-race-condition-causing-problems-when-trying-to-parallelize-a-loop-used-to-populate-an-array/117733/2):
>
> The issue is not a race condition. I didn’t see one during a quick read. You have misunderstood the fundamental difference between using processes (via Distributed.jl) and threads. You cannot just slap @distributed on the code and expect it to just work. You need to think about moving data between workers. Let me try to explain: Workers have totally different memory spaces on your machine. In fact for all practical purposes they could live on physically different machines. So when you preallo…

So yes a `SharedArray` is a valid answer, if all processes live on the same physical machine. However if that is the case, do you have a reason to use processes instead of threads? Threads have generally less overhead and are easier to work with (e.g. your code example would’ve just worked).

---

_[View the full topic](https://discourse.julialang.org/t/using-distributed-for-loop/117831)._
