4 ms·
I'd argue that the main benefits of day-to-day async programming isn't performance but actually the concurrency patterns that help you sequence your code and re
by hombre_fatal 6y ago
I'd argue that the main benefits of day-to-day async programming isn't performance but actually the concurrency patterns that help you sequence your code and resource access in ways that `thread { work() }` could not.
For example, future/result combinators and `await [task1, task2.then(task3)]`.
- gpderetta 6y agoyou could (and can in many languages) use futures with threads just fine though.
- lmm 6y agoNot really - it gets very messy because every time you transform a future you have to figure out where you're getting the thread for that transformation to run on.
- gpderetta 6y agosorry, what transformation? A future is simply a placeholder for something being computed asynchronously. On a threadful design you would simply spawn a thread (or pick one from a thread pool) to handle the computation. Normally your future runtime would handle it for you. Basically you end up with something similar to the fork-join model.
- lmm 6y ago> sorry, what transformation? Whenever you want to transform a result that's in a future, e.g. you have a future for a number and want to add 2 to it. > On a threadful design you would simply spawn a thread (or pick one from a thread pool) to handle the computation. If you allow yourself to spawn threads everywhere you'll quickly run out of resources. So you have to manage which thread pool you're using where and ensure you're not bringing in priority inversions etc.. It's really not that easy. > Basically you end up with something similar to the fork-join model. The fork-join model isn't really a purely thread-based model - the work-stealing technique is pretty much trying to reimplement what async-style code would do naturally.
- mlyle 6y ago> ensure you're not bringing in priority inversions etc.. Remember we're comparing to async/futures, which are not really guaranteed to not starve either. At least with thread pools you can, in theory, manage this well.
- lmm 6y ago> Remember we're comparing to async/futures, which are not really guaranteed to not starve either. At least with thread pools you can, in theory, manage this well. With async/futures you're giving the runtime control over these decisions, whereas with threads you're managing them yourself, which can be an advantage but only if you don't make errors with that manual control. An async/future runtime can know which tasks are waiting for which other tasks, letting it avoid deadlocks and a lot of possible priority inversions, and the async style naturally lends itself to writing code that's logically end-to-end (on a single "fiber" even as that fiber moves between threads), which means there's less need to balance resources across multiple thread pools.
- gpderetta 6y agoIf you want to add 2 to a future, you block and then extract the content and add to it. Re spawning threads, depending on the semantics of your framework you cana always execute async operations sychronously.
- lmm 6y ago> If you want to add 2 to a future, you block and then extract the content and add to it. That makes using futures completely pointless. Why not just write blocking code if you're going to block anyway?
- gpderetta 6y agoPipelining and opportunity for parallelization: A = computeA() B = computeB() C = A+B Vs futA = asyncComputeA() futB = asyncComputeB() A, B = waitAll(futA, futB) C = A+B