There are several threading models that are used for scheduling threads within applications:
- 1:1 (kernel level threading): Each thread created by a user is mapped to a scheduled thread in the kernel.
- N:1 (user level threading): All the threads that are created by a user in a single application are actually mapped to a single scheduled kernel thread.
- M:N (hybrid threading): M threads created by a user in an application are mapped to N kernel threads.
User-level threads are considered faster than kernel-level threads, because context switching at the kernel level is more expensive than in user-level. One big disadvantage of user-level threads is that they don't utilize multiprocessor systems as they only use one kernel-level thread.
There are some articles telling that M:N threading model is best used with N as the number of CPU cores (here is an example). In this way we can achieve the advantage of both 1:1 and N:1 threading models.
My questions are:
- When we use kernel-level threads we also get 'extra' time slice during execution (as opposed to user-level threads), so doesn't it make up for the slow context-switch?
- Why is the number of CPU cores even relevant here? As I understand, the number of CPU cores is pretty transparent here, because even when we use the exact amount kernel-threads, nothing guarantee us that they are really executed simultaneously, because other cores can execute other threads from other processes, and 'our' threads might still use context switch afterwards. So they use context switch regardless of the how many CPU cores we have. Am I wrong here?.