Tech Jay
Tech Jay

Reputation: 434

No of threads on a machine and ThreadPool thread count

I am new to the concept of threads. I have read that the number of threads for a machine depend on number of cores of the machine i.e. if a machine has 2 cores it will have 4 threads, 4 cores than 8 threads and so on. Is my understanding correct that the total number of threads in machine depend on number of cores only? If yes, if we talk about threadpool in .net framework, can a threadpool have more than 16 threads (for an 8 core machine)?

Upvotes: 0

Views: 281

Answers (1)

Giorgi Tsiklauri
Giorgi Tsiklauri

Reputation: 11110

Threads can be created even in the case of 1 core CPU, i.e. n (n>1) number of threads can be running on just 1 core CPU (this is how it was done in old days).

In Multi-threading, what often happens is that Threads are concurrent to each other, they compete for one resource, but this is done in such a short time, that they, sort of, seem virtually parallel to each other. CPU switching happens in nanoseconds or milliseconds.

Threads can run in a really parallel mode, but this means, that at least 2 independent cores of CPU should be at your disposal. Yet, again: scheduling of what runs where and when depends on CPU Scheduling in Operating System.

You should understand that CPU executes a bare binary instructions and it knows nothing more. So, when application is loaded into memory, its threads represent just a bunch of machine-code instructions, which can be executed slice by slice, one after another, in particular scheduling, with some halts, or just in parallel. This all (again) depends on the design of OS and CPU.

As said above, often times, several threads may be running on 1 core and CPU Scheduling will be allocating corresponding time slices for each thread, correspondingly threads will compete to each other for one core. In this case, CPU jumps from execution some part of instructions from one thread, to execution of some other part in some other thread, and this continues until the threads are alive.

If your CPU has several cores, real parallelism can be achieved, but you [almost] never control this, Operating System and CPU does.

In contrast, see also how single thread may be running on different cores. As we've said above, execution of some chunk of instructions is scheduled by CPU and OS.

Upvotes: 1

Related Questions