Why would concurrency using std::async be faster than using std::thread?
23
1
I was reading Chapter 8 of the "Modern C++ Programming Cookbook, 2nd edition" on concurrency and stumbled upon something that puzzles me. The author implements different versions of parallel map and reduce functions using std::thread and std::async . The implementations are really close; for example, the heart of the parallel_map functions are // parallel_map using std::async ... tasks.emplace_back(std::async( std::launch::async, [=, &f] {std::transform(begin, last, begin, std::forward<F>(f)); })); ... // parallel_map using std::thread ... threads.emplace_back([=, &f] {std::transform(begin, last, begin, std::forward<F>(f)); }); ... The complete code can be found here for std::thread and there for std::async . What puzzles m...