https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

thomasnield

04/24/2017, 1:28 PM
I'm curious what is the difference between lightweight threads and threads? I haven't really played with Qasar and I'm just starting to play with coroutines. What makes them less expensive? Are they just a better abstraction? Are they backed and distributed to traditional threads?
d

dalexander

04/24/2017, 1:30 PM
Lightweight threads probably refer to “fibers” or “green threads”. There are usually many fibers running on a single OS thread, and because OS threads are relatively expensive to create, you can have many more fibers than you can OS threads.
t

thomasnield

04/24/2017, 1:31 PM
So this is not an abstraction but rather the opposite. it's going deeper into the OS resources at a more granular level?
d

dalexander

04/24/2017, 1:32 PM
It’s sort of an abstraction. So you have many fibers per one OS thread. So if you have a lot of co-routines (hundreds or thousands), it doesn’t require allocations hundreds or thousands of OS threads, which are pretty expensive (typically 1MB of stack memory to begin with).
The OS doesn’t know anything about fibers.
https://en.wikipedia.org/wiki/Fiber_(computer_science) here’s the wikipedia article for more information 🙂
t

thomasnield

04/24/2017, 1:43 PM
Fascinating, thanks
g

groostav

04/26/2017, 7:37 AM
@thomasnield I think the way to think of a fiber is as a job, where the platform provides us with a single static job queue, and threads are the things executing those jobs. Given such a system, one job might in turn enqueue three more jobs. With some compiler magic, you can actually get one method spread into multiple sequential jobs, each one enquing the next, which is where the coroutine compiler support in kotlin comes in.
4 Views