std::basic_syncbuf
std::basic_syncbuf (동기화 출력 버퍼)
std::basic_streambuf의 래퍼로, 출력을 자체 내부 버퍼에 누적했다가 파괴·명시적 요청 시 전체 내용을 감싼 버퍼에 원자적으로 전달해 연속된 문자 시퀀스로 보이게 해요. 데이터 레이스와 문자 섞임을 보장 없이 방지해요. C++20부터 있어요.
출처: cppreference
본문
<syncstream> 헤더에 정의돼 있어요.
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
> class basic_syncbuf : public std::basic_streambuf<CharT, Traits>;
std::basic_syncbuf는 std::basic_streambuf(생성 시 포인터로 제공)의 래퍼예요. 출력을 자체 내부 버퍼에 누적하고, 파괴 시·명시적으로 요청될 때 전체 내용을 감싼 버퍼에 원자적으로 전달해 연속된 문자 시퀀스로 보이게 해요. 데이터 레이스와 문자 섞임이 없음을 보장해요.
공통 문자 타입 typedef
std::syncbuf—std::basic_syncbuf<char>std::wsyncbuf—std::basic_syncbuf<wchar_t>
멤버 함수
- (constructor) — syncbuf를 생성해요.
- get_wrapped — 감싼 버퍼를 돌려줘요.
- emit — 전체 내용을 감싼 버퍼에 전달해요.
- rdbuf — 감싼 버퍼 접근.
예제
#include <syncstream>
#include <iostream>
int main()
{
std::osyncstream out(std::cout);
out << "Hello, ";
out << "sync world!\n";
// 파괴 시(또는 emit) 한 번에 std::cout 으로 전달됨
}