std::basic_osyncstream<CharT,Traits,Allocator>:: basic_osyncstream
From cppreference.net
<
cpp
|
io
|
basic osyncstream
|
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) | |
构造新的同步输出流。
5)
移动构造函数。从
other
的对应子对象移动构造
std::
basic_ostream
基类和 std::basic_syncbuf 成员,随后调用
set_rdbuf
并传入新构造的底层
std::
basic_syncbuf
指针以完成基类的初始化。在此移动构造函数执行后,
other.
get_wrapped
(
)
将返回
nullptr
,且
other
的析构不会产生任何输出。
参数
| buf | - | 指向将被包装的 std::basic_streambuf 的指针 |
| os | - | 对 std::basic_ostream 的引用,其 rdbuf() 将被包装 |
| a | - | 传递给成员 std::basic_syncbuf 构造函数的分配器 |
| other | - | 用于移动来源的另一个 osyncstream |
示例
运行此代码
#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)}; } }
可能的输出:
Synchronized output should not cause any interference: ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ████████████████████████████████████████████████████████████████████████████████████████████████████ Lack of synchronization may cause some interference on output: ████▓▓██▒▒▒▒▓▓██░░▒▒██░░▒▒░░░░▒▒░░▓▓▒▒██░░████████████▓▓██████▓▓▒▒▓▓██░░████▓▓▓▓██▒▒░░░░░░░░▓▓░░▓▓██▒▒▒▒▒▒▒▒▓▓██▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▒▒▒▒░░▒▒▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▒▒▒██░░░░░░░░▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓████████▓▓▓▓▓▓▓▓▓▓▓▓░░▓▓▓▓ ▒▒▒▒██░░██████████████████████████░░░░░░░░░░░░░░██░░▒▒░░░░░░██████████████████ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒ ░░░░░░
参见
构造
basic_syncbuf
对象
(
std::basic_syncbuf<CharT,Traits,Allocator>
的公开成员函数)
|