6 ms·
Why is the cost of switching threads so much higher than the cost of switching fibers?
by mac01021 7y ago
Why is the cost of switching threads so much higher than the cost of switching fibers?
- fwsgonzo 7y agoBecause threads are traditionally created and scheduled by the OS, so it inevitably involves a costly context switch both first into the kernel and then back again into the next thread, if one is ready. Userspace threads are more light-weight, but probably still worse than just using fibers and co-routines. Depends on your needs, I suppose.
- gpderetta 7y agoSwitching threads require entering the kernel which costs from a few hundreds to thousands of clock cycles (and it got worse from all the spectre/meltdown mitigations). A fiber switch can be done in less than 10 clock cycles.