std::basic_..._osyncstream(3)
π NAME π
okay so this page is std::basic_..._osyncstream(3), from the C++ Standard Libary manual (yep, that spelling is whatβs in the header, bestie π«Ά). Itβs all about the constructor std::basic_osyncstream::basic_osyncstream β std::basic_osyncstream::basic_osyncstream β and itβs giving main-character energy, no cap. β¨
β¨ Synopsis β¨
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 a new synchronized output stream. π
1-4)Constructs the private memberstd::basic_syncbufusing the buffer and the allocator provided, and initializes the base class with a pointer to the memberstd::basic_streambuf.5)Move constructor. Move-constructs thestd::basic_ostreambase and thestd::basic_syncbufmember from the corresponding subobjects ofother, then callsset_rdbufwith the pointer to the newly-constructed underlyingstd::basic_syncbufto complete the initialization of the base. After this move constructor,other.get_wrapped()returnsnullptrand destruction ofotherproduces no output.
π Parameters π
bufβ pointer to thestd::basic_streambufthat will be wrapped.osβ reference to astd::basic_ostream, whoserdbuf()will be wrapped.aβ the allocator to pass to the constructor of the memberstd::basic_syncbuf.otherβ anotherosyncstreamto move from.
π§ Example π§
// Run this code
#include <iostream>
#include <string_view>
#include <syncstream>
#include <thread>
void worker(const int id, std::ostream &os)
{
std::string_view block;
switch (id)
{
default: [[fallthrough]];
case 0: block = "ββ";
break;
case 1: block = "ββ";
break;
case 2: block = "ββ";
break;
case 3: 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:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββ
π See also π
constructorβ constructs abasic_syncbufobject (public member function ofstd::basic_syncbuf<CharT,Traits,Allocator>).
okay bestie, thatβs the whole page π β cute little footer sign-off: source/version/date is http://cppreference.com Β· 2024.06.10 Β· page std::basic_..._osyncstream(3). Slay. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms