In this section, we are going to implement some functions that simulate computation-intensive tasks that depend on each other, and let them run as parallel as possible:
- Let's first include all the necessary headers:
#include <iostream> #include <iomanip> #include <thread> #include <string> #include <sstream> #include <future> using namespace std; using namespace chrono_literals;
- We need to synchronize concurrent access to cout, so let's use the synchronization helper from the other recipe in this chapter:
struct pcout : public stringstream { static inline mutex cout_mutex; ~pcout() { lock_guard<mutex> l {cout_mutex}; cout << rdbuf(); cout.flush(); } };
- Now let's implement three functions which transform strings. ...