Coroutines don’t need synchronization in contrary to threads because:
- There can only be one coroutine running since the event loop and all coroutines run in single thread
- Coroutines don’t get interrupted by the OS while executing and have to explicitly yield control back to the event loop with
awaitkeyword - Coroutines can only be cancelled when they are suspended at
awaitstatement. This means cancelled coroutine can do cleanup when they receiveCancelledErrorexception because they will be scheduled to run again and do whatever cleanup steps before returning if they catch the exception