# @threads uses only half the number of nthreads()

**URL:** https://discourse.julialang.org/t/threads-uses-only-half-the-number-of-nthreads/54058
**Category:** General Usage
**Tags:** question, multithreading, threads
**Created:** [January 27, 2021, 2:29pm UTC](https://discourse.julialang.org/t/threads-uses-only-half-the-number-of-nthreads/54058 "2021-01-27T14:29:56Z")
**Posts on this page:** 1
**Showing post:** 13

<div class="post-metadata">

### Author: ![Chris\_Green](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_green/32/32265_2.png) [@Chris\_Green](https://discourse.julialang.org/u/Chris_Green)
#### Post date: [October 12, 2021, 12:58am UTC](https://discourse.julialang.org/t/threads-uses-only-half-the-number-of-nthreads/54058/13 "2021-10-12T00:58:07Z")

</div>

This is a well known misfeature of the windows OS that needs a minor bit of code at thread creation time to work around. thread libraries like intel TBB do this for you, but otherwise you need a few lines of code.  
You can see an article about it here, including how to fix it in c code:

> **[Thread pools and Windows processor groups](https://chrisgreendevelopmentblog.wordpress.com/2017/08/29/thread-pools-and-windows-processor-groups/)**
>
> I needed to write a basic worker thread pool implementation. I needed a simple system that let me queue jobs, have those jobs executed by worker threads, and wait on job completion. I looked at a f…

In my test case, there are 250 windows threads created by Julia as I asked, perhaps by using pthreads which I assume lacks this code as well.

When they run in a tight loop that is not memory bound, you will see 256 non-idle threads, but only 1/4 CPU usage in the task manager. Using any good windows diagnostic tool, you can see that julia has created 250 threads, but they are **all assigned to the same thread group** instead of using the 4 groups that are present in this machine. _Thus, the 256 OS threads only use 64 HW threads out of the 256 available_

If you run a third party program called “process lasso”, it can be told to “spread the threads out over all the processor groups” for a running process. Applying this to julia makes it able to take advantage of all the cores. You will see that now the process manager shows 100% CPU usage on all cores, and that you get corresponding speedups.

I managed to get a 150x speedup vs single threaded code on this 128/256 core system by doing this.

Memory bandwidth doesn’t figure into this. Even if the threads were horribly memory bound, they would consume 100% of all of the cpus, though they might be spending more time servicing cache misses then doing math, and so not see a scalable speedup.

Its an easy problem to repro, and an easy one tix if you have a system to test on.

---

_[View the full topic](https://discourse.julialang.org/t/threads-uses-only-half-the-number-of-nthreads/54058)._
