Synchronized Output Streams with C++20

Contents[Show]

What happens when you write without synchronization to std::cout? You get a mess. With C++20, this should not be anymore.

TimelineCpp20

Before I present synchronized output streams with C++20, I want to show non-synchronized output in C++11.

 

// coutUnsynchronized.cpp

#include <chrono>
#include <iostream>
#include <thread>

class Worker{
public:
  Worker(std::string n):name(n) {};
    void operator() (){
      for (int i = 1; i <= 3; ++i) {
        // begin work
        std::this_thread::sleep_for(std::chrono::milliseconds(200));      // (3)
        // end work
        std::cout << name << ": " << "Work " << i << " done !!!" << '\n'; // (4)
      }
    }
private:
  std::string name;
};


int main() {

  std::cout << '\n';
  
  std::cout << "Boss: Let's start working.\n\n";
 
  std::thread herb= std::thread(Worker("Herb"));                        // (1)
  std::thread andrei= std::thread(Worker("  Andrei"));
  std::thread scott= std::thread(Worker("    Scott"));
  std::thread bjarne= std::thread(Worker("      Bjarne"));
  std::thread bart= std::thread(Worker("        Bart"));
  std::thread jenne= std::thread(Worker("          Jenne"));            // (2)
  
  
  herb.join();
  andrei.join();
  scott.join();
  bjarne.join();
  bart.join();
  jenne.join();
  
  std::cout << "\n" << "Boss: Let's go home." << '\n';                   // (5)
  
  std::cout << '\n';
  
}

 

The boss has six workers (lines 1 - 2). Each worker has to take care of three work packages that take 1/5 second each (line 3). After the worker is done with his work package, he screams out loudly to the boss (line 4). Once the boss receives notifications from all workers, he sends them home (line 5).
What a mess for such a simple workflow! Each worker screams out his message ignoring his coworkers!

coutUnsynchronized

  • std::cout is thread-safe: The C++11 standard guarantees that you need not protect std::cout. Each character is written atomically. More output statements like those in the example may interleave. This interleaving is only a visual issue; the program is well-defined. This remark is valid for all global stream objects. Insertion to and extraction from global stream objects (std::cout, std::cin, std::cerr, and std::clog) is thread-safe. To put it more formally: writing to std::cout is not participating in a data race, but does create a race condition. This means that the output depends on the interleaving of threads. Read more about the terms data race and race condition in my previous post: Race Conditions versus Data Races.

How can we solve this issue? With C++11, the answer is straightforward: use a lock such as std::lock_guard to synchronize the access to std::cout. For more information about locks in C++11, read my previous post Prefer Locks to Mutexes.

// coutSynchronized.cpp

#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>

std::mutex coutMutex;                                                 // (1)

class Worker{
public:
  Worker(std::string n):name(n) {};
 
    void operator() (){
      for (int i = 1; i <= 3; ++i) { 
        // begin work
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
        // end work
        std::lock_guard<std::mutex> coutLock(coutMutex);             // (2)
        std::cout << name << ": " << "Work " << i << " done !!!" << '\n';
      }                                                              // (3)
    }
private:
  std::string name;
};


int main() {

  std::cout << '\n';
  
  std::cout << "Boss: Let's start working." << "\n\n";
 
  std::thread herb= std::thread(Worker("Herb"));
  std::thread andrei= std::thread(Worker("  Andrei"));
  std::thread scott= std::thread(Worker("    Scott"));
  std::thread bjarne= std::thread(Worker("      Bjarne"));
  std::thread bart= std::thread(Worker("        Bart"));
  std::thread jenne= std::thread(Worker("          Jenne"));
  
  herb.join();
  andrei.join();
  scott.join();
  bjarne.join();
  bart.join();
  jenne.join();
  
  std::cout << "\n" << "Boss: Let's go home." << '\n';
  
  std::cout << '\n';

}

 

The coutMutex in line (1) protects the shared object std::cout. Putting the coutMutex into a std::lock_guard guarantees that the coutMutex is locked in the constructor (line 2) and unlocked in the destructor (line 3) of the std::lock_guard. Thanks to the coutMutex guarded by the coutLock the mess becomes a harmony.

coutSynchronized

With C++20, writing synchronized to std::cout is a piece of cake. std::basic_syncbuf is a wrapper for a std::basic_streambuf. It accumulates output in its buffer. The wrapper sets its content to the wrapped buffer when it is destructed. Consequently, the content appears as a contiguous sequence of characters, and no interleaving of characters can happen.
Thanks to std::basic_osyncstream, you can directly write synchronously to std::cout by using a named synchronized output stream.
Here is how the previous program coutUnsynchronized.cpp is refactored to write synchronized to std::cout. So far, only GCC 11 supports synchronized output streams.

 

// synchronizedOutput.cpp

#include <chrono>
#include <iostream>
#include <syncstream>
#include <thread>

class Worker{
public:
  Worker(std::string n): name(n) {};
    void operator() (){
      for (int i = 1; i <= 3; ++i) {
        // begin work
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
        // end work
        std::osyncstream syncStream(std::cout);                    // (1)
        syncStream << name << ": " << "Work " << i                 // (3)
<< " done !!!" << '\n'; } // (2) } private: std::string name; }; int main() { std::cout << '\n'; std::cout << "Boss: Let's start working.\n\n"; std::thread herb= std::thread(Worker("Herb")); std::thread andrei= std::thread(Worker(" Andrei")); std::thread scott= std::thread(Worker(" Scott")); std::thread bjarne= std::thread(Worker(" Bjarne")); std::thread bart= std::thread(Worker(" Bart")); std::thread jenne= std::thread(Worker(" Jenne")); herb.join(); andrei.join(); scott.join(); bjarne.join(); bart.join(); jenne.join(); std::cout << "\n" << "Boss: Let's go home." << '\n'; std::cout << '\n'; }

 

The only change to the previous program coutUnsynchronized.cpp is that std::cout is wrapped in a std::osyncstream (line 1). When the std::osyncstream goes out of scope in line (2), the characters are transferred and std::cout is flushed. It is worth mentioning that the std::cout calls in the main program do not introduce a data race and, therefore, need not be synchronized. The output happens before or after the output of the threads.


Because I use the syncStream declared on line (3) only once, a temporary object may be more appropriate. The following code snippet presents the modified call operator:

void operator()() {
  for (int i = 1; i <= 3; ++i) { 
    // begin work
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    // end work
    std::osyncstream(std::cout) << name << ": " << "Work " << i << " done !!!" 
                                << '\n';
  }
}

std::basic_osyncstream syncStream offers two interesting member functions.

    • syncStream.emit() emits all buffered output and executes all pending flushes.
    • syncStream.get_wrapped() returns a pointer to the wrapped buffer.

cppreference.com shows how you can sequence the output of different output streams with the get_wrapped member function.

// sequenceOutput.cpp

#include <syncstream>
#include <iostream>
int main() {
  
  std::osyncstream bout1(std::cout);
  bout1 << "Hello, ";
  {
      std::osyncstream(bout1.get_wrapped()) << "Goodbye, " << "Planet!" << '\n';
  } // emits the contents of the temporary buffer
  
  bout1 << "World!" << '\n';
  
} // emits the contents of bout1

sequenceOutput

What's next?

Wow! Now I'm done with C++20. I have written about 70 posts to C++20. You can have more information on C++20 in my book: C++20: Get the Details.

But there is still one feature, I want to give more insight into coroutines. In my next posts, I start to play with the new keywords co_return, co_yield, and co_await.

 

 

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, and Evangelos Denaxas.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, and Richard Sargeant.

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

 

Comments   

0 #1 juhani 2021-03-02 15:34
I think "must not" should be "need not" in the sentence about C++11 cout thread safety. Now it sounds like the compiler is required to reject the program if you try to protect cout with a mutex :-D

I'm not English, but I learned the difference the hard way a long time ago when I failed a mission in a video game because of misreading it as a relaxation on objectives: "must not" != "muss nicht" (My German may be rusty, but back then it was better than my English.)
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 3656

Yesterday 8041

Week 19280

Month 78909

All 6307381

Currently are 283 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments