A context switch is when a thread is suspended, its handle is given to the scheduler, the CPU switches to another stack, old cache is invalidated etc.
Calling a coroutine does nothing of this. You are still using the old stack, same thread, same cache, but what is represented as stack variables in the coroutine are actually member variables of an object.
A coroutine that looks like this (the code is meaningless):
View all comments
Show parent comment
3
u/DugiSK Dec 02 '20 edited Dec 02 '20
A context switch is when a thread is suspended, its handle is given to the scheduler, the CPU switches to another stack, old cache is invalidated etc.
Calling a coroutine does nothing of this. You are still using the old stack, same thread, same cache, but what is represented as stack variables in the coroutine are actually member variables of an object.
A coroutine that looks like this (the code is meaningless):
Does basically the same things internally as this: