Task-based parallelism in C++
A task is a computation that can be potentially executed concurrently with other computations. A thread is a system-level representation of a task. In the previous chapter, we learned how to execute a task concurrently with other tasks launched by constructing an std::thread object with the task as its argument to the constructor. A task can be any callable object such as a function, Lambda, or a functor. But this approach of executing a function concurrently using std::thread is called a thread-based approach. The preferred choice for concurrent execution is a task-based approach, and this will be discussed in this chapter. The advantage of a task-based approach over a thread-based approach is to operate at the (higher) conceptual level of tasks rather than directly at the lower level of threads and locks. Task-based parallelism is achieved by following standard library features:
- Future and promise for returning a value from a task associated with a separate thread
- packaged_task to help launch tasks and provide a mechanism for returning a result
- async() for launching a task similar to a function call