I think this variable don't show the maximum number of threads I can run in my pc, I was running a program in C++ and actually I could initialise 2000 threads, even the htop command showed the number of threads increasing up to 2000 more. But the variable only returns 8, what means that my computer can execute in parallel up to 8 tasks, but parallelism is different from concurrency, am I'm right? I'm a noob in the concept of concurrency. I even asked chat gpt and he agrees with me.
-
["Returns the number of concurrent threads supported by the implementation. The value should be considered only a hint."](https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency). All threads aren't necessarily concurrent. Is that your question? – Nelfeal Jul 26 '23 at 10:04
-
2From [this reference](https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency): "The value should be considered only a hint." My guess is that your implementation reports the number of CPU cores in your system. You can have a lot more threads than cores. – Some programmer dude Jul 26 '23 at 10:04
-
1Related question: [Optimal number of threads per core](https://stackoverflow.com/questions/1718465/optimal-number-of-threads-per-core) – Homer512 Jul 26 '23 at 10:07
-
1No it does not show the maximum number of threads you can create, it shows (or tries to show) how many threads you can run concurrently. – Pepijn Kramer Jul 26 '23 at 10:17
-
_"but parallelism is different from concurrency"_ Typical definitions really say that concurrency does not mean that threads are executed at the same time. But this term is frequently used this way. We may also think of _hardware concurrency_ as of _parallelism_. BTW, the standard says that `std::hardware_concurrency` returns _"the number of hardware thread contexts"_. I believe C++ works with _concurrency_ since it does not provide any guarantees about parallel execution (we can run multithreaded C++ program on a single-core CPU). – Daniel Langr Jul 26 '23 at 10:24
-
An 8 core processor can run 8 thread in parallel but more than that can be run concurrently thanks to the scheduling of the OS (running your program with 8 thread will not stop your browser from running). Some processors have logical cores (also called hardware threads) so they can execute more (software) threads in parallel (though threads belonging to the same core partially share the resources of the target core). Regarding concurrency vs parallelism, please read [this famous post](https://stackoverflow.com/questions/1050222/what-is-the-difference-between-concurrency-and-parallelism). – Jérôme Richard Jul 26 '23 at 10:29
-
Possible search term _"preemptive multitasking"_. – Richard Critten Jul 26 '23 at 11:02
2 Answers
You can start any number of threads. They will be managed by the operating system (that's like "software threads"). However, your hardware can only run a limited number of threads at the same time (that's like "hardware threads").
If you observe limits in the number of threads (like 2000) will be a limit of the operating system. You'd need to consult the documentation or books for the reasons there might be for such a limit. Most likely they are a result of some implementation decision.
So, how can 2000 threads run on a 8 core machine? I'll try to keep it simple.
There is a timer clock built into your computer. That timer will cause a hardware interrupt. This will signal the processor to save its state and start an interrupt service routine (ISR). So that thread lost the control over the CPU.
We call this non-exclusive access to the CPU "preemption". Maybe you have heard the term "preemptive multitasking".
The ISR can then decide whether some other thread should be executed. So one of the 2000 threads will get control over the CPU. After some time, the timer will cause an interrupt again and the process repeats.
How the OS decides which of the 2000 threads to choose is a complex topic and requires several pages in books like "Windows Internals".
my computer can execute in parallel up to 8 tasks
I wouldn't use the word "task" here. One thread can possibly execute several tasks while it's running.
parallelism is different from concurrency
I don't see developers distinguish these terms clearly. They often interleave. The degree of parallelism is certainly the number of threads running on hardware.

- 55,411
- 20
- 125
- 222
-
Obviously a good science popularization. So the function returns the number of cores in the CPU? – LiuYuan Jul 26 '23 at 10:48
-
1@LiuYuan: yes, if that value can be figured out (some hardware might not support it) https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency – Thomas Weller Jul 26 '23 at 11:21
-
1@LiuYuan - Or the number of cores you can expect to use. On a mainframe with hundreds of cores, you might not be allowed use all of them yourself! – BoP Jul 26 '23 at 13:04
TLDR:
Two threads are concurrent with each other when both have been started but neither one of them has finished.
Two threads are running in parallel with each other when each of them is actively using cycles on a different CPU core during the same interval of time.
IMO, hardware_concurrency
is a horrible name. That function does not tell you how many concurrent threads you can have. It tells you how many of your threads can be running in parallel at any given moment in time. (I.e., it tells you how many CPU cores are available to execute your threads.)

- 25,130
- 5
- 37
- 57
-
What name would you recommend? Note that with [hyperthreading](https://www.hp.com/us-en/shop/tech-takes/what-is-hyperthreading#:~:text=Hyper%2Dthreading%20is%20a%20process,threads%20or%204%20virtual%20cores.), a single core can run two threads in parallel. – Pete Becker Jul 26 '23 at 16:14
-
@PeteBecker, If I'd been there, I might have suggested having two functions: `num_physical_cpu_cores()` and `num_logical_cpu_cores()`. I might also have suggested that, for example, `num_physical_cpu_cores()` is a cover for `num_cpu_cores(CORETYPE_PHYSICAL)`, etc. – Solomon Slow Jul 26 '23 at 16:27