Question

Most of the modern languages support coroutine for concurrency. They usually refer their coroutine as a light weight thread that you can spawn tens of thousands with only a little footprint. Since "thread is too heavy", why don't linux kernel support coroutine directly inside thread rather than letting the programming languages / VM support it?

Was it helpful?

Solution

Threads are considered heavyweight exactly because they are implemented in the kernel. Every context switch from one thread to another requires interaction with the kernel. This is why modern languages (e.g. Go) create co-routines in user space, scheduling them onto OS threads in the run-time system.

This hybrid setup allows further simplifications. E.g. co-routines can perform cooperative multitasking, yielding to the scheduler only when they encounter a blocking operation (as defined by the language). They will still use multiple cores when the internal scheduler uses multiple threads to run its co-routines, without bothering the kernel to schedule tens of thousands of threads and keeping track of which ones are blocked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top