skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
hpp-syncstream
This page is read only. You can view the source, but not change it.
# <syncstream> **[<syncstream>](https://en.cppreference.com/w/cpp/header/syncstream)** provides `std::osyncstream`, a wrapper around `std::ostream` that buffers output and flushes it atomically, preventing interleaved writes from multiple threads (C++20). Use it when multiple threads write to the same stream and you want each logical message to be atomic. This example uses osyncstream from multiple threads to write atomic messages to stdout without interleaving. ```cpp // compile: g++ -std=c++20 -pthread -o syncstreamexample syncstreamexample.cpp // run: ./syncstreamexample // description: synchronized output from multiple threads #include <syncstream> #include <iostream> #include <thread> int main() { std::osyncstream out(std::cout); auto worker = [](int id) { std::osyncstream local(std::cout); local << "thread " << id << " message\n"; }; std::thread t1(worker, 1); std::thread t2(worker, 2); t1.join(); t2.join(); return 0; } ```
Page Tools
Show pagesource
Backlinks
Back to top