A thread is the unit of execution within a process. A process can have anywhere from one thread to many.
A process can have more than one thread, and these threads are managed independently by the scheduler. All the threads within one process are interrelated to each other. Threads have some common information, such as data segment, code segment, files, etc., that is shared to their peer threads.
The kernel parameter threads-max controls the maximum number of threads. This parameter is defined in the file /proc/sys/kernel/threads-max. Here, the output 63704 indicates that the kernel can execute a maximum of 63,704 threads.
Whenever Windows experiences more than 64 threads in a system, it separates those threads into processor groups. The way this is done is very rudimentary: of the enumerated cores and threads, the first 64 go into the first group, the second 64 go into the next group, and so on.
The prevailing consensus is that having more physical cores is preferable to having more threads. In comparison, a CPU with 8 cores and 8 threads would perform better than one with 2 cores and 8 threads. However, the more threads our CPU can manage, the better it will perform while multitasking.
If you create thousands of threads then you will waste time context switching between them and your work will take longer to complete. Instead of manually starting new threads you should use the thread pool to perform your work so Windows itself can balance the optimum number of threads.
Often I see people asking why they can't create more than around 2000 threads in a process. The reason is not that there is any particular limit inherent in Windows. Rather, the programmer failed to take into account the amount of address space each thread uses.
Instance methods are synchronized over the instance of the class owning the method, which means only one thread per instance of the class can execute this method.
Many modern processors support hyperthreading: each physical core behaves as if it is actually two cores, so it can run two threads simultaneously (e.g. execute one thread while the other is waiting on a cache miss).
You can have many threads because a processor core can execute instructions on one thread for awhile, and then switch to another thread, executing some instructions there. This process occurs rapidly and continuously, making it appear that all threads are executing simultaneously.
If one thread crashes due to a segmentation fault or other error, all other threads and the entire process are killed.
A process, in the simplest terms, is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread.
A single processor can run only one instruction at a time: it is impossible to run more programs at the same time. A program might need some resource, such as an input device, which has a large delay, or a program might start some slow operation, such as sending output to a printer.
If a CPU has 8 cores with two threads per core, it will have 16 threads to perform tasks. Multithreading allows a CPU to execute multiple threads of code and run concurrent tasks of a process at the same time.
One thread will work on one request until it's done. A thread that's working can never be paused and then given another task.
Is 4 threads 1 core possible? Yes, it is. One example is the Xeon Phi 7235 . It has 64 cores and 256 threads.
A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread.
I would recommend starting with at least an eight-core CPU, with 16 cores currently being the sweet spot. More than 16 (e.g. with Threadripper PROs up to 64 cores) comes with a single-core performance hit, so while rendering will be faster, other workloads like active work will start to suffer.
Investigate an app that is responding to requests slowly. Use the dotnet-counters tool to identify ThreadPool starvation is likely occurring. Use the dotnet-stack tool to determine what work is keeping the ThreadPool threads busy.
Hyperthreading splits each physical CPU core into two virtual cores. Physical CPU cores are more powerful than virtual cores. Hyperthreading is important for high-end software, but not as much for everyday programs.
Managing threads
Creating a process is far more expensive, because the entire parent process addressing space is duplicated. The threads library API is also easier to use than the library for managing processes. Thread creation requires only the pthread_create subroutine.
The clear downside to many-to-many and many-to-one is that kernel mode becomes a resource whose unavailability can cause one group of threads to block another group of threads.
“Threads are cheap, but they're not free.” Too many threads will cause a system to spend lots of time context switching and not doing useful work. Each thread requires memory for stack space and TCBs. Too many threads and these memory uses will dominate overall memory use.