3 ms·
If 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 c
by gpderetta 6y ago
If 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
- lmm 6y agoThat's almost certainly a bad idea in a web server context like this article is talking about. You improve best-case latency when the server's not loaded, but now you're using 3 threads per request to get a less than 2x speedup (and in a bigger example it would be worse), so your scaling behaviour will get worse.
- gpderetta 6y agoalways spawning a thread is of course the naive implementation. You can put an upper bound on the number of threads and fallback to synchronous execution of async operations in the worst case (for example inside the wait call). If your threads are a bit more than dumb os threads (say, an hybrid M:N scheduler) you can do smarter scheduling, including work stealing of course.
- lmm 6y agoWell, as your threads become less like threads and more like a future/async runtime you come closer to the advantages and disadvantages of a future/async runtime, yes.
- gpderetta 6y agoThe underlying thread model have always been 'async' in some form under the hood, i.e. at some point there is always a multiplexer/scheduler that schedules continuations. Normally this is inside the kernel, but M:N or purely superspace based thread models have been used for decades. Really the only difference between the modern async model and other 'threaded' model is its 'stacklessness' nature. This is both a major problem (due to the green/red function issue and not being able to abstract away asynchronicity) and an advantage (due to the guaranteed fixed stack size, and, IMHO overrated, ability to identify yield points). At the end of the day is always continuations all the way down.