cpp/io/basic osyncstream/basic osyncstream: Difference between revisions
From cppreference.com
Created page with "{{cpp/io/basic_osyncstream/title | basic_osyncstream}} {{cpp/io/basic_osyncstream/navbar}} {{dcl begin}} {{dcl | num=1 | 1= basic_osyncstream( streambuf_type* buf, const Alloc..." |
Andreas Krug (talk | contribs) m fmt, {{c}}, headers sorted |
||
| (8 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
{{cpp/io/basic_osyncstream/title | basic_osyncstream}} | {{cpp/io/basic_osyncstream/title|basic_osyncstream}} | ||
{{cpp/io/basic_osyncstream/navbar}} | {{cpp/io/basic_osyncstream/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl | num=1 | 1= | {{dcl|num=1|1= | ||
basic_osyncstream( streambuf_type* buf, const Allocator& a); | basic_osyncstream( streambuf_type* buf, const Allocator& a ); | ||
}} | }} | ||
{{dcl | num=2 | 1= | {{dcl|num=2|1= | ||
explicit basic_osyncstream( streambuf_type* buf ) | explicit basic_osyncstream( streambuf_type* buf ) | ||
}} | }} | ||
{{dcl | num=3 | 1= | {{dcl|num=3|1= | ||
basic_osyncstream( std::basic_ostream<CharT, Traits>& os, const Allocator& a) | basic_osyncstream( std::basic_ostream<CharT, Traits>& os, const Allocator& a ) | ||
}} | }} | ||
{{dcl | num=4 | 1= | {{dcl|num=4|1= | ||
explicit basic_osyncstream( std::basic_ostream<CharT, Traits>& os ) | explicit basic_osyncstream( std::basic_ostream<CharT, Traits>& os ) | ||
}} | }} | ||
{{dcl | num=5 | {{dcl|num=5|1= | ||
basic_osyncstream( std::basic_osyncstream&& other) noexcept; | basic_osyncstream( std::basic_osyncstream&& other ) noexcept; | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Constructs new synchronized output stream | Constructs new synchronized output stream | ||
@1-4@ | @1-4@ the private member std::basic_syncbufusing the buffer and the allocator provided, and initializes the base class with a pointer to the member std::basic_streambuf. | ||
@5@ Move constructor. Move-constructs the base | @5@ Move constructor. Move-constructs the base and the std::basic_syncbuf from the corresponding subobjects of {{|other}}, then calls {{|set_rdbuf}} with the pointer to the newly-constructed std::basic_syncbuf. After this move constructor, {{c|other.get_wrapped()}} returns {{c|nullptr}} . | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
{{par | buf | pointer to the {{lc|std::basic_streambuf}} that will be wrapped }} | {{par|buf|pointer to the {{lc|std::basic_streambuf}} that will be wrapped}} | ||
{{par | os | reference to a {{lc|std::basic_ostream}}, whose rdbuf() will be wrapped}} | {{par|os|reference to a {{lc|std::basic_ostream}}, whose rdbuf() will be wrapped}} | ||
{{par | a | the allocator to pass to the constructor of the member {{lc|std::basic_syncbuf}} }} | {{par|a|the allocator to pass to the constructor of the member {{lc|std::basic_syncbuf}}}} | ||
{{par | other | another osyncstream to move from}} | {{par|other|another osyncstream to move from}} | ||
{{par end}} | {{par end}} | ||
===Example=== | ===Example=== | ||
{{example}} | {{example | ||
}} | |||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
{{dsc inc | cpp/io/basic_syncbuf/dsc constructor}} | {{dsc inc|cpp/io/basic_syncbuf/dsc constructor}} | ||
{{dsc end}} | {{dsc end}} | ||
{{langlinks|zh}} | {{langlinks|zh}} | ||
Latest revision as of 17:09, 9 September 2023
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.
1-4) Constructs the private member
std::basic_syncbuf using the buffer and the allocator provided, and initializes the base class with a pointer to the member std::basic_streambuf.5) Move constructor. Move-constructs the
std::basic_ostream base and the std::basic_syncbuf member from the corresponding subobjects of other, then calls set_rdbuf with the pointer to the newly-constructed underlying std::basic_syncbuf to complete the initialization of the base. After this move constructor, other.get_wrapped() returns nullptr and destruction of other produces no output.Parameters
| buf | - | pointer to the std::basic_streambuf that will be wrapped |
| os | - | reference to a std::basic_ostream, whose rdbuf() will be wrapped |
| a | - | the allocator to pass to the constructor of the member std::basic_syncbuf |
| other | - | another osyncstream to 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
constructs a basic_syncbuf object (public member function of std::basic_syncbuf<CharT,Traits,Allocator>)
|