How Coroutines does context switching?
Coroutines provide a way to achieve cooperative multitasking within a single thread. Unlike threads, which are managed by the operating system and can be preemptively switched, coroutines require explicit yielding of control between each other. The context switching in coroutines is typically implemented using a technique called “stackful” or “stackful” context switching.
Here’s a simplified explanation of how context switching works in coroutines:
1. Coroutine Execution: Coroutines are executed within a single thread. Each coroutine has its own stack, which stores the state of the coroutine’s execution, including local variables and the point where it was suspended.
2. Coroutine Suspension: When a coroutine encounters a suspension point, such as an `await` statement or a `yield` statement, it voluntarily suspends its execution and yields control back to the scheduler or the calling coroutine.
3. Scheduler: The scheduler is responsible for managing the execution of coroutines. It maintains a queue of coroutines ready to be executed. When a coroutine suspends, the scheduler is notified and it decides which coroutine to run next.
4. Context Switching: When the scheduler selects a new coroutine to run, it performs a context switch. A context switch involves saving the state of the currently running coroutine (including its stack) and restoring the state of the next coroutine to be executed.
5. Stack Preservation: Since coroutines have their own stack, the context switch must save and restore the stack state. This is typically done by saving the current stack pointer and restoring it when resuming a coroutine. The scheduler keeps track of the necessary stack information for each coroutine.
6. Resuming Execution: After the context switch, the execution resumes from the point where the coroutine was suspended, with the state of its stack intact. The coroutine continues executing until it encounters another suspension point or completes its execution.
7. Synchronization: To ensure proper synchronization and prevent race conditions, coroutines usually use synchronization primitives like locks, semaphores, or channels. These primitives help manage shared resources and coordinate the execution of coroutines.
It’s important to note that the exact implementation details of context switching in coroutines can vary depending on the programming language or framework being used. Some programming languages provide built-in support for coroutines and handle the context switching internally, while others require the use of external libraries or frameworks to manage coroutines and context switching.