Sharing data during parallel execution
Think in terms of tasks rather than threads (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cp4-think-in-terms-of-tasks-rather-than-threads).
Referring back to the C++ Core Guidelines, they advise us that it is better to stick to tasks rather than threads. A thread is a technical implementation idea, a perspective on how the machine works. On the other hand, a task is a practical concept for work that you want to do, ideally alongside other tasks. In general, practical concepts are simpler to understand and provide better abstraction, and we prefer them.
But what is a task in C++? Is it another standard library primitive or what? Let’s have a look!
In C++, besides threads, tasks are also available to perform work asynchronously. A task consists of a worker and two associated components: a promise and a future. These components are connected through a shared state, which is a kind of data channel. The promise does...