O'Reilly logo

C++17 STL Cookbook by Jacek Galowicz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

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:

  1. 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;
  1. 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();          }      };
  1. Now let's implement three functions which transform strings. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required