Reading the “Kotlin Coroutines” book, it just dawn...
# coroutines
a
Reading the “Kotlin Coroutines” book, it just dawned on me that
CoroutineContext
is basically the coroutines’ equivalent of
ThreadLocal
. Why don’t the tutorials say this? Would make understanding so much faster…
mind blown 2
I think the two paradigms (threads vs coroutines) are different enough that drawing that comparison might just create more confusion
a
I agree, it’s not equivalent. Maybe “similar” is better.
👍 1
I was just completely confused as to what a coroutine context was until the parallel to threadlocal dawned on me.
Not sure I agree with that article. Can’t you think of the chain of `Continuation`s are the coroutine?
s
You'd have to take that up with Roman 😄. He probably knows what he's talking about, though, he was the project lead on coroutines and now on all of Kotlin
If I had to take a stab at an answer, I'd say that the chain of continuations is to the coroutine as the call stack is to the thread
a
Yes. But what is a thread if not its call stack + a record somewhere in the thread scheduler?
s
Actually here is a StackOverflow post, also from Roman, where he says that "Coroutine's analog to
ThreadLocal
is `CoroutineContext`". So maybe this is something that could be mentioned in the documentation. I agree with you that it would give a good starting point for understanding, even if the comparison is inexact.
👌 1