3 ms·
> Why implement coroutines when you have multithreading? Because coroutines are easier to think about than threads, and avoid entire (large) classes of bugs.
by com2kid 4y ago
> Why implement coroutines when you have multithreading?
Because coroutines are easier to think about than threads, and avoid entire (large) classes of bugs.
Because coroutines (typically) have a lower overhead than threads.
Because if you are IO bound and not CPU bound, and therefore you don't need to spread work across cores, threads may not provide a given workload with much, or any, benefit.
- sgtnoodle 4y agoI've benefited from cooperative userspace context switching with CPU bound workloads as well. Imaging simulating a vehicle's worth of embedded software, with several dozen tasks originally intended to run at 100Hz asynchronously across multiple different computers. Now, imagine running them faster than real time, deterministically. Now imagine doing several thousand of those simulations. The overhead of OS threads, context switching and synchronization starts to add up, and is 10-100x slower for that sort of workload.
- com2kid 4y agoCooperative multitasking systems having more deterministic performance profiles is often overlooked. I'm working on a post right now covering this in detail, but I spent the last 2 hours being reminded that explaining concurrency stuff in simple terms is hard. :-D