Namespaces
Variants

std::basic_osyncstream<CharT,Traits,Allocator>:: basic_osyncstream

From cppreference.net
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) 제공된 버퍼와 할당자를 사용하여 private 멤버 std:: basic_syncbuf 를 구성하고, 기본 클래스를 해당 멤버에 대한 포인터로 초기화합니다.
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> 의 public member function)