TimelineCpp20

Synchronized Output Streams with C++20

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, please read my previous post Prefer Locks to Mutexes.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (open)
  • "Generic Programming (Templates) with C++": October 2024
  • "Embedded Programming with Modern C++": October 2024
  • "Clean Code: Best Practices for Modern C++": March 2025
  • Do you want to stay informed: Subscribe.

     

    // 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 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 characters can interleave.
    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 on 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 will 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, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery, Matt Godbolt, and Honey Sukesan.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Modernes C++ GmbH

    Modernes C++ Mentoring (English)

    Do you want to stay informed about my mentoring programs? Subscribe Here

    Rainer Grimm
    Yalovastraße 20
    72108 Rottenburg

    Mobil: +49 176 5506 5086
    Mail: schulung@ModernesCpp.de
    Mentoring: www.ModernesCpp.org

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *