# Does multithreading require a new process?

**URL:** https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056
**Category:** General Usage
**Created:** [August 16, 2020, 6:35pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056 "2020-08-16T18:35:12Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [August 16, 2020, 6:35pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/1 "2020-08-16T18:35:12Z")

</div>

I’m looking at the new multithreading features (vs. multiprocess). Based on the documentation, it looks like currently you cannot generate new threads in a running Julia process? That is, they need to be specified as a startup argument or environment variable?

[https://docs.julialang.org/en/v1/manual/multi-threading/](https://docs.julialang.org/en/v1/manual/multi-threading/)

If some functions allow you to create a thread and then destroy it on completion, that isn’t clear from the documentation.

If it requires a new process, is there a plan to allow creation and destruction of threads within a process?

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [August 17, 2020, 1:41pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/2 "2020-08-17T13:41:03Z")

</div>

In Julia I don’t believe there is ever a reason for more threads than CPU cores. This is because Julia basically uses cooperative multitasking, which means that if a Task doesn’t need to use the CPU it lets another Task use it.

If you have 4 CPU cores with 4 threads and 4 Tasks using 100% of the CPU you are doing calculations as fast as you can. If you bump that up to 8 threads and 8 Tasks using 100% you will actually do calculations slower. This is because the OS will swap the threads onto the CPU to ensure that all threads get equal time on the CPU (50% of the CPU). This swap is **not** free, thread state has to be saved and restored, and the CPU cache will be flushed since the new thread will probably not be accessing the same memory that the old thread was.

The down side to this is when creating a “desktop app” you can’t have a “UI thread” that just handles responses to user actions.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [August 17, 2020, 1:53pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/3 "2020-08-17T13:53:57Z")

</div>

Julia multithreading works by having Julia create N operating system threads at initialization, and then when you want to run something in parallel you @spawn a task… this task will get bound to a specific thread, and execute in parallel with other tasks on different threads.

it’s not clear to me how tasks get scheduled on a given thread and tasks can’t migrate from thread to thread **yet**.

I actually think you should set the JULIA\_NUM\_THREADS to be the same as your number of actual cores plus a small constant, like 2 or 3. Then you can have a few threads to do UI and file IO type stuff and the OS will take care of scheduling them, without tying up your computation threads.

once Julia can migrate tasks across threads this would be maybe less important.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [August 17, 2020, 1:57pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/4 "2020-08-17T13:57:00Z")

</div>

See [https://github.com/JuliaLang/julia/issues/16134](https://github.com/JuliaLang/julia/issues/16134).

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [August 17, 2020, 8:20pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/5 "2020-08-17T20:20:04Z")

</div>

Thanks all. I’m thinking of how to provide a package that can provide multithreaded performance without the user having to know about that as part of their setup. If a user has to set an environment variable, that’s one more barrier to use.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [August 17, 2020, 8:22pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/6 "2020-08-17T20:22:47Z")

</div>

> [@BioTurboNick](#):
>
> If a user has to set an environment variable, that’s one more barrier to use.

In Julia 1.5 you can start julia with `--threads N` also.

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [August 17, 2020, 8:29pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/7 "2020-08-17T20:29:49Z")

</div>

I suspect most naive users will probably interact with the Julia REPL without ever typing the command line. Atom, VS Code, or Start Menu shortcut launch it directly.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [August 17, 2020, 8:37pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/8 "2020-08-17T20:37:54Z")

</div>

Note that Atom states with a number of threads equal to physical cores I think by default, vscode has a setting for this also but it seems blank by default

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [August 17, 2020, 8:41pm UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/9 "2020-08-17T20:41:12Z")

</div>

Ah, I’ve been using VSCode lately, seems to default to 1.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [August 18, 2020, 6:52am UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/10 "2020-08-18T06:52:06Z")

</div>

Maybe @pfitzseb who has now worked on both editors can comment on why the default differs between them

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [August 18, 2020, 7:23am UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/11 "2020-08-18T07:23:31Z")

</div>

Well, we just haven’t gotten around to adding that logic to VSCode (in the hopes that Julia might support a `JULIA_NUM_THREADS=auto` setting or something like that) 🙂

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [August 18, 2020, 8:14am UTC](https://discourse.julialang.org/t/does-multithreading-require-a-new-process/45056/12 "2020-08-18T08:14:02Z")

</div>

Cool thanks - I just wanted to see whether there’s anything with the way the VSCode extension runs that makes putting this setting to 4 or whatever less innocuous than it was in Juno.
