2 ms·
Can you explain why ?
by fooyc 10y ago
Can you explain why ?
- lucozade 10y agoBecause synchronous IO will block the OS thread. If you're multiplexing a bunch of green threads on that OS thread, they'll all stall. That's why Go, for example, does use async IO. But it makes the use of it look like sync IO for simplicity. In the runtime, however, it is doing all the async event management so that you don't have to. From a programmer POV this is a good thing, but it comes at the price of a runtime that has to exist. Something that Rust eschews.
- Rusky 10y agoSibling comment by lucozade is good. To be more specific, it's because OS-level threads are the only way the kernel gives applications to run any code at all. When an OS-level thread makes a syscall, it ceases to run any application code until that syscall completes- it's just an ordinary function call that also switches to kernel mode. The OS kernel itself provides both blocking and asynchronous syscalls. So if your OS-level thread is switching between several coroutines, and one of them makes a synchronous read() syscall, the whole OS-level thread blocks and can't run any other coroutines until the read completes. If it instead makes an asynchronous syscall, the kernel returns immediately and the OS-level thread can switch to another coroutine while the first one waits.