I/O manipulators | ||||
Print functions(C++23) | ||||
C-style I/O | ||||
Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
Streams | ||||
Abstractions | ||||
File I/O | ||||
String I/O | ||||
Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
Synchronized Output | ||||
(C++20) | ||||
Types | ||||
Error category interface | ||||
(C++11) | ||||
(C++11) |
Public member functions | ||||
basic_osyncstream::basic_osyncstream (C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) |
basic_osyncstream( streambuf_type* buf,const Allocator& a); | (1) | |
explicit basic_osyncstream( streambuf_type* buf); | (2) | |
basic_osyncstream(std::basic_ostream<CharT, Traits>& os,const Allocator& a); | (3) | |
explicit basic_osyncstream(std::basic_ostream<CharT, Traits>& os); | (4) | |
basic_osyncstream(std::basic_osyncstream&& other)noexcept; | (5) | |
Constructs new synchronized output stream.
buf | - | pointer to thestd::basic_streambuf that will be wrapped |
os | - | reference to astd::basic_ostream, whose rdbuf() will be wrapped |
a | - | the allocator to pass to the constructor of the memberstd::basic_syncbuf |
other | - | another osyncstream to move from |
#include <iostream>#include <string_view>#include <syncstream>#include <thread> void worker(constint id,std::ostream&os){std::string_view block;switch(id){default:[[fallthrough]];case0: block="██";break;case1: block="▓▓";break;case2: block="▒▒";break;case3: block="░░";break;}for(int i=1; i<=50;++i) os<< block<<std::flush; os<<std::endl;} int main(){std::cout<<"Synchronized output should not cause any interference:"<<std::endl;{auto scout1=std::osyncstream{std::cout};auto scout2=std::osyncstream{std::cout};auto scout3=std::osyncstream{std::cout};auto scout4=std::osyncstream{std::cout};auto w1=std::jthread{worker,0,std::ref(scout1)};auto w2=std::jthread{worker,1,std::ref(scout2)};auto w3=std::jthread{worker,2,std::ref(scout3)};auto w4=std::jthread{worker,3,std::ref(scout4)};} std::cout<<"\nLack of synchronization may cause some interference on output:"<<std::endl;{auto w1=std::jthread{worker,0,std::ref(std::cout)};auto w2=std::jthread{worker,1,std::ref(std::cout)};auto w3=std::jthread{worker,2,std::ref(std::cout)};auto w4=std::jthread{worker,3,std::ref(std::cout)};}}
Possible output:
Synchronized output should not cause any interference:░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓████████████████████████████████████████████████████████████████████████████████████████████████████ Lack of synchronization may cause some interference on output:████▓▓██▒▒▒▒▓▓██░░▒▒██░░▒▒░░░░▒▒░░▓▓▒▒██░░████████████▓▓██████▓▓▒▒▓▓██░░████▓▓▓▓██▒▒░░░░░░░░▓▓░░▓▓██▒▒▒▒▒▒▒▒▓▓██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▒▒▒▒░░▒▒▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▒▒▒██░░░░░░░░▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓████████▓▓▓▓▓▓▓▓▓▓▓▓░░▓▓▓▓▒▒▒▒██░░██████████████████████████░░░░░░░░░░░░░░██░░▒▒░░░░░░██████████████████▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒░░░░░░
constructs abasic_syncbuf object(public member function of std::basic_syncbuf<CharT,Traits,Allocator> )[edit] |