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 a program just like in the recipe before, but this time with multiple producers and multiple consumers:

  1. First, we need to include all needed headers and we declare that we use namespace std and chrono_literals:
      #include <iostream>      #include <iomanip>      #include <sstream>      #include <vector>      #include <queue>      #include <thread>      #include <mutex>      #include <condition_variable>      #include <chrono>           using namespace std;      using namespace chrono_literals;
  1. Then we implement the synchronized printing helper from the other recipe in this chapter because we are going to do a lot of concurrent printing:
      struct pcout : public stringstream {          static inline mutex cout_mutex;          ~pcout() { lock_guard<mutex> ...

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