What happens when a single process runs more threads than the CPU has? The scenario is that I have a process scanning two directories for matching files. OS is Windows 11. The program can have the user device how many threads to give to each directory scan. I have decided to allocate 8 threads to each directory scan, so 16 threads in total for the process. The CPU has 8 cores and 16 threads in total. What would happen if the process was allocated 32 threads, for example? Windows would schedule it... but how? Since these threads are perfectly fungible (they all are performing the exact same task), how would Windows decide which threads should be given priority, and on what basis? What happens to the other 16 threads? Paused entirely? Execution is alternated so half are paused, the others are executed, and then vice versa?
Generally speaking, there are two kinds of processes in this CPU bound, and IO bound. Since you are doing IO, this process is probably IO bound. That means that the threads spend most of their time blocked waiting on the IO subsystems to give them data to process. So, most of the time, all 32 threads are paused entirely while waiting for the IO requests to return.
This is why IO bound processes tend to preform well with many many threads. The limit at hand being the bandwidth and latency. If those are large enough, it can be worthwhile to have hundreds of threads. (For spinning disks, they can typically service one request at a time, so, if you are accessing one, you want n+1 threads where n is the number of disks, or to use asynchronous IO) Your processor, on the other hand, will be mostly idle.
For CPU bound tasks, given an eight core processor, with hyperthreading*, the operating system can schedule up to 16 threads for simultaneous execution, meaning you want as many threads as there are logical processors in the system. Any extra threads (say from other processes or ready io threads) will be paused waiting for the operating system to preform a context switch.
Threads are generally scheduled for blocks of time known as quantums; so, normally, a thread will be permitted to run uninterrupted for a quantum or three (somewhere between 30 and a few hundred ms) or until some device receives input and interrupts the processor. Then the timer on that core will run out, the executing thread will be interrupted, and control will be returned to the scheduler which, if there is another ready thread of the same priority, will save off the context and schedule the next ready thread.
In Windows, ready threads are scheduled in a round robin fashion by priority. So, higher priority threads are always scheduled before lower priority threads, and threads with a given priority are scheduled one after another in a cycle. If a higher priority thread becomes ready, the lower priority thread will be preempted and the higher priority thread permitted to run. If a thread is a thread for the foreground process, it gets scheduled for three times as long as threads for background processes do.
* Hyperthreading allows each core to switch back and forth between two threads of execution, interleaving their operations to allow for things like more efficient branch prediction and dealing with other timing based issues that might otherwise cause for parts of the core to starve for work while waiting for an operation to complete.
(For way too much information about windows scheduling, https://www.microsoftpressstore.com/art ... ?p=2233328 is an interesting article).