User Space vs Kernel Space Threads
| User Space threads | Kernel Space threads | |||
|---|---|---|---|---|
| What |
A user-space thread is any thread whose scheduling is primarily
handled in user space instead of Kernel Space. Kernel does
not know these threads exist. The runtime may schedule User threads over: - one kernel thread (M:1) - several kernel threads (M:N) - or even dedicate one kernel thread per task (less common).
|
Threads created and scheduled inside kernel space. | ||
| Can we create more threads than CPU cores? |
Yes. User space threads are not aware about CPU cores. |
Yes. On 64 cored machine, 1000's of kernel threads can be created,
context switching will happen between them. At any instant 64 threads can run simultaneously. Remaining threads stay in the runnable queue. |
||
| Examples |
C++20 Coroutines, boost::fibers Rust: tokio::spawn() Golang: go routines |
C++: pthread_create(), std::thread class
Rust: std::thread::spawn()
|
||
| Speed | Fast. No context switch, trap instruction, memory cache flush needed. | Slow. Context switch, trap instruction, memory cache flush needed | ||
| Disadvantages | if User space thread blocks on kernel call, whole process will block |
User Space threads
Green Threads
Green threads are user space threads but Created and scheduled entirely
by a language runtime
Name came from Green Team at Sun microsystems who implemented them in
java
go routines(M:N model)
Goroutines are user-space threads, but not classical green threads.
10000 Goroutines
|
Go Scheduler
|
v
200 Kernel Threads
|
v
CPU cores