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) | |
新しい同期出力ストリームを構築します。
1-4)
提供されたバッファとアロケータを使用して非公開メンバ
std::
basic_syncbuf
を構築し、基底クラスをこのメンバへのポインタで初期化します
std::
basic_streambuf
。
5)
ムーブコンストラクタ。
std::
basic_ostream
基底とstd::basic_syncbufメンバを
other
の対応する部分オブジェクトからムーブ構築し、その後
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>
の
公開メンバ関数)
|