# Multi-threading or multi-processing, how to know which to use and when?

**URL:** <https://discourse.julialang.org/t/multi-threading-or-multi-processing-how-to-know-which-to-use-and-when/72278>\
**Category:** Performance\
**Tags:** question, parallel, multithreading, distributed\
**Created:** [November 30, 2021, 8:42am UTC](https://discourse.julialang.org/t/multi-threading-or-multi-processing-how-to-know-which-to-use-and-when/72278 "2021-11-30T08:42:50Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 30, 2021, 9:05am UTC](https://discourse.julialang.org/t/multi-threading-or-multi-processing-how-to-know-which-to-use-and-when/72278/3 "2021-11-30T09:05:44Z")

</div>

> [@Lamma](#):
>
> I settled on using `@threads` however i am new the julia so I am entirely unsure if `Distributed` would be more optimal here.

Generally speaking, multithreading is more light-weigt than multiprocessing. It’s cheaper to start a thread than a process and as a user you don’t have to get into the `@everywhere` business. So if you’re not concerned about distributed computing involving multiple machines, multithreading should likely be your first choice. However, what I like about `Distributed` is that processes are “very much separate” conceptually and actually. For this reason, one typically doesn’t run into race conditions that easily (they can still exist of course).

> [@Lamma](#):
>
> My understanding of the difference is that `distributed` computing would run multiple processes on different cores whilst `@threads` will split the task up into subtasks and run on the same (or different cores)?

Unless you explicitly pin threads or processes, you shouldn’t make any assumptions about on which core a certain thread or process will run. The OS can move threads and process around. (In fact, on macOS you can’t even pin at all!)

> [@Lamma](#):
>
> Does this then mean `Distributed` will run multiple iterations of the for loop at once?

Well, both multithreading as well as multiprocessing give you parallelism. I guess I don’t understand the question?

---

_[View the full topic](https://discourse.julialang.org/t/multi-threading-or-multi-processing-how-to-know-which-to-use-and-when/72278)._
