CppCon 2019: Eric Niebler, Daisy Hollman “A Unifying Abstraction for Async in C++”
Why Stack Overflow recommends it
Stack Overflow users link it when explaining why std::future is slow or limited and where C++ async (executors, sender/receiver, P2300) is heading instead.
Recommended by 3 people across 4 different months, 2020 to 2024.
What it is
A CppCon 2019 talk by Eric Niebler and Daisy Hollman on the Standard Committee's work on Executors and the Sender/Receiver model, proposed as a unifying abstraction for asynchronous programming in C++. It argues that std::future, promises and std::async are inefficient or broken, and walks through design examples and connections to coroutines.
What people said
Recommends Barry · 2020-08 · Stack Overflow · on “Where is std::future::then() and the concurrency TS?”
Has the Concurrency TS been dropped? Yes. P1445 [LINK] discusses this, SG1 voted to withdraw the TS. Note that atomic smart pointers, latches, and barriers are in C++20. As far as improving future goes, the direction there is called executors and the design currently being iterated on is P0443 [LINK] , which allows for lazy composition of work in a way that would be much more efficient than future::then . Two of the authors gave a talk on this at CppCon [this video] that goes into the issues pretty well.
Recommends tirimatangi · 2020-10 · Stack Overflow · on “Using boost::future with "then" continuations”
There may be a faster and easier way to use continuations than std::future or boost::future. Both of them have been criticized for being slow due to several reasons. See e.g. this presentation [this video] . The solution proposed in the presentation is implemented in github [LINK] as a header-only library. You can chain any number of continuations and avoid implicit heap allocations. Also exceptions can be caught normally. Here is an example where two sets of continuations are run in parallel: #include <iostream> #include <cmath> #include <string> #include "Lazy.h" int main() { int iInput = 10; // Future #1: input an integer, then take square root, then convert double to string auto f = Lazy::fut
Recommends Enlico · 2024-08 · Stack Overflow · on “How is the new asynchronous model in C++26 different from existing models?”
I'm curious how they differ from other models such as primitive std::future and std::promise , executors, and Boost's ASIO? Not sure what you mean by "executors" if not the very proposal you refer to, nor I know about Boost ASIO, but in regards to how P2300 [LINK] (the proposal at the basis of that part of the draft you refer to) relates to std::future and std::promise , the difference is precisely that those two are very low level building blocks of what Eric Niebler calls "unstructured concurrency". With those building blocks, a lot of responsibility falls on you, as a programmer, in terms of managing lifetimes (of states, callbacks, ...). P2300 [LINK] is the basis of structured concurrenc
Recommends Enlico · 2024-09 · Stack Overflow · on “A formal+didactic+schematic explanation of concurrency vs parallelism”
tl;dr I think I have understood the concurrency vs parallelism examples below, but only really to the extent of those examples, and I'd like to get a more formal explanation that would allow (or help) me to understand and digest this concept to the point that I don't have to remember the two examples below to (re-)convince myself everytime of the difference, but that I can just make up such examples, because I've truly understood it in "mathemtatical" terms. In this presentation at CppCon 2019 [this video] Daisy Hollman shows this snippet of concurrent code, std::atomic<int> x = {0}; // worker A while (x.load() == 0) {} cout << "Hello" << endl; // worker B x.store(1); and compares it with one way
Cites tirimatangi · 2020-10 · Stack Overflow · on “call a function many times in parallel”
In github [LINK] there is a header-only library which can be used for calling a function asynchronously once for each element of a vector. If the function returns a value, it gives back the results in another vector. It avoids using std::future which has been criticized e.g. in this [this video] CppCon presentation for being slow. Also exceptions can be caught normally. Here is an example: #include <iostream> #include "Lazy.h" template <class T1, class T2> double foo(T1 t1, T2 t2) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); return double(t1) + double(t2); } int main() { constexpr int maxThreads = 16; // 0 means as many threads as there are cores // Make an input vector std::ve
Cites tirimatangi · 2020-10 · Stack Overflow · on “std::async doesn't parallelize tasks”
std::future has been criticized e.g. in this [this video] CppCon presentation for being slow. You can avoid std::async and std:future entirely by using this header-only library [LINK] . You can run any number of functions asynchronously and get the results as a tuple. Also exceptions can be caught normally. Here is an example: #include <iostream> #include "Lazy.h" template <class T> T foo(T x) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); return x + 10.5; } int main() { int input = 54; try { auto [i, d, c] = Lazy::runParallel( [&](){ return foo(int(input)); }, [&](){ return foo(double(input)); }, [&](){ return foo(char(input)); } ); std::cout << "foo(int) = " << i << ", foo(doub