3

My CPU is an i5-8400, which has 6 cores and 6 threads. What does this mean? Initially, I thought it meant that I had 6 threads available per core, which totals 36 threads.

Say I'm making a C program, where I create pthreads, does that mean I can only have 6 threads in that program, as its process will run on a single CPU core? If that's the case, what would happen if I tried creating a seventh thread?

When I go to task manager (windows), I see thousands of threads:

enter image description here

, which means my understanding was wrong.

So my questions are:

  1. How does my CPU number of threads relate to how many threads I can create in a process, i.e., say I create a C program; how many threads can I create in its process?
  2. What happens if I try to create a thread, and there are no more threads available?
  • 4
    A CPU has cores - it does not have threads. Processes have threads. If your CPU has 6 cores, 6 threads can run _simultaneously_ but many threads are idle much of the time, waiting for I/O, say, and even if they're logically active the O/S can do time sharing between them. The total number of threads that you can have is not limited by the CPU but by the amount of free memory as each thread needs room for a stack and other data structures. – 500 - Internal Server Error Jul 13 '21 at 14:50
  • 1
    Your i5-8400 is not hyperthreaded so, one concurrant thread per core is what you get. – Ted Lyngmo Jul 13 '21 at 14:52
  • @500-InternalServerError yeah, that's what I was afraid of hearing. Then, following the link I posted (about my CPU), why does it say `# of threads: 6`? –  Jul 13 '21 at 14:53
  • 1
    It _is_ 6 - one per core. Many CPU:s have hyperthreading which gives them 2 threads per core. Yours don't. – Ted Lyngmo Jul 13 '21 at 14:53
  • If I understand this right then, If my process runs on a single CPU core, only one thread can run at a time in that process, is that right? –  Jul 13 '21 at 14:56
  • 1
    I wouldn't mix in processes here but yes, only one thread can run at the same time in one of your i5-8400 cores. 6 threads is still pretty awesome. I ran CounterStrike and World of Tanks with 2 threads on a Pentium 4. :-) – Ted Lyngmo Jul 13 '21 at 14:57
  • 1
    @500-InternalServerError In fact each core might have several threads on hardware level. Take a look at this [question](https://askubuntu.com/questions/668538/cores-vs-threads-how-many-threads-should-i-run-on-this-machine) for instance or at [AMDs Threadripper specs](https://www.cpu-world.com/CPUs/Zen/AMD-Ryzen%20Threadripper%203990X.html) which states the number of cores and number of threads. Of course it is not the same as the OS thread, which can be scheduled on or off one of the hardware thread. – Eugene Sh. Jul 13 '21 at 15:00
  • @TedLyngmo that makes sense. Just one more question then. If only one thread can be executed at a time in a single core, if a process is executing in a single core, there cannot be any performance boost by using multiple threads, as they'll execute separately, is that right? –  Jul 13 '21 at 15:02
  • 1
    @DuarteArribas Your process' may wander around and execute in different cores during its lifetime (unless you've specified it to have affinity for a certain core). If you have support for 6 threads in hardware, you can make use of them all. – Ted Lyngmo Jul 13 '21 at 15:04
  • 1
    that makes sense, thanks! –  Jul 13 '21 at 15:06
  • Thousands...... – Martin James Jul 13 '21 at 18:17

1 Answers1

3

Intel CPU has cores an multiple execution unit per core. A mainboard can have many CPU.

For example, my system has 2 Intel Xeon E5620 processors. Each Xeon E5620 has 4 cores and each core can execute 2 threads (That is the hyperthreading feature). On my system, a total of 2x4x2=16 threads can be execute really simultaneously).

The difference between the number of threads and the number of cores exists because CPU has incomplete core which is able to execute multiple threads but less performing than a complete core. To say it otherwise, it is faster to have 8 cores single thread than 4 cores each double thread.

When we talk about the number of thread in CPU context, it means that you can have so much thread really executing in parallel. While you look at the task manager, you see the total number of thread objects in the system. At a given moment, most of them are sleeping (for example waiting for I/O or a mutex,...) only the number of thread given for the CPU times the number of CPU can be really executing instructions.

If you create more threads than is available in the available CPU, then part of them are simply waiting for their turn to execute. A CPU thread execute one after the other the existing threads. Actually the operating system has a scheduler that determine which thread is ready to run or not.

Interesting readings:

fpiette
  • 11,983
  • 1
  • 24
  • 46