# What is the difference between \`Threads.@spawn\` and \`Distributed.@spawnnat\`

**URL:** <https://discourse.julialang.org/t/what-is-the-difference-between-threads-spawn-and-distributed-spawnnat/35560>\
**Category:** General Usage\
**Tags:** question, parallel\
**Created:** [March 5, 2020, 8:27am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-threads-spawn-and-distributed-spawnnat/35560 "2020-03-05T08:27:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [March 5, 2020, 8:27am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-threads-spawn-and-distributed-spawnnat/35560/1 "2020-03-05T08:27:07Z")

</div>

trying to get a better understanding on Julia’s parallelization possibilities I heard about the awesome `@spawn` macro which is new in Julia 1.3

Looking more into the available tools I am confused about the difference between `Threads.@spawn` and `Distributed.@spawnnat`.  
What is a typical usecase for each? What may be anit-patterns respectively?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 5, 2020, 8:35am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-threads-spawn-and-distributed-spawnnat/35560/2 "2020-03-05T08:35:46Z")

</div>

Distributed is for running computation on _workers_, i.e., separate instances of julia running either locally or remote. Threads are local to your CPU. Threads have less overhead, but require you to think about thread safety. Distributed computing scales to many machines, clusters, the cloud etc., but communication between the main process and the workers can limit the performance for some tasks and code must be available on all participating machines etc.

I hope that gives you an idea.

Threads are used for stuff like

- Matrix multiplication
- Speeding up loops involving somewhat expensive computations.

Distributed for stuff like:

- Many parallel MC simulations that take long time each
- Very expensive computations that take very long time in relation to how long time it takes to send data to the worker.
- Very large, distributed linear algebra or optimization

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 5, 2020, 8:40am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-threads-spawn-and-distributed-spawnnat/35560/3 "2020-03-05T08:40:32Z")

</div>

I have some slides here if you want to learn more  
[https://github.com/baggepinnen/juliacourse/blob/master/lecture5/distributed.pdf](https://github.com/baggepinnen/juliacourse/blob/master/lecture5/distributed.pdf)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 5, 2020, 9:37am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-threads-spawn-and-distributed-spawnnat/35560/4 "2020-03-05T09:37:40Z")

</div>

Please consider adding a paragraph to the very beginning of the docs [Parallel Computing · The Julia Language](https://docs.julialang.org/en/v1/manual/parallel-computing/) explaining this (pretty much what you wrote above).

I think it would help users unfamiliar with these concepts navigate this chapter of the docs better.
