Synchronized Output Streams with C++20
This is a cross-post from www.ModernesCpp.com.
What happens when you write without synchronization to std::cout? You get a mess. With C++20, this should not be anymore.
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!
- std::cout is thread-safe: The C++11 standard guarantees that you must 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 become a harmony.
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
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, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang G?rtner, Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, and Tobi Heideman.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, and Richard Sargeant.
My special thanks to Embarcadero
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)
Deutsch
- Embedded Programmierung mit modernem C++: 12.04.2021 - 14.04.2021, Termingarantie
- Clean Code mit modernem C++: 22.06.2021 - 24.06.2021
English
- Workshop: Concepts in C++20: 22.03.2021 (17:00 - 21:00 CET)
- Workshop: Coroutines in C++20: 06.04.2021 (17:00 - 21:00 CEST)
Standard Seminars
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ - The Core Language
- C++ - The Standard Library
- C++ - Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Patterns and Architecture Patterns with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
New
Contact Me
- Tel.: +49 7472 917441
- Mobil: +49 152 31965939
- Mail: [email protected]
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net