◐ Shell
clean mode source ↗

std::basic_osyncstream - cppreference.com

来自cppreference.com

template<
    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>
> class basic_osyncstream : public std::basic_ostream<CharT, Traits>
(C++20 起)

类模板 std::basic_osyncstreamstd::basic_syncbuf 的便利包装。它提供机制以对写入同一流的各线程进行同步。

它能用作具名变量:

{
    std::osyncstream sync_out(std::cout); // std::cout 的同步包装
    sync_out << "Hello, ";
    sync_out << "World!";
    sync_out << std::endl; // 标记冲洗,但仍未进行
    sync_out << "and more!\n";
} // 传输各字符并冲洗 std::cout

还有临时量:

std::osyncstream(std::cout) << "Hello, " << "World!" << '\n';

它提供保证:所有对最终目标缓冲区(上例中是 std::cout)作出的输出将免除数据竞争,而且将不以任何方式穿插或截断,只要每次对该最终目标缓冲区的写入都通过 std::basic_osyncstream(可以不同的)实例进行。

std::basic_osyncstream 的典型实现仅保有一个数据成员:被包装的 std::basic_syncbuf

cpp/io/ios basecpp/io/basic ioscpp/io/basic ostream

继承图

提供了几个针对常用字符类型的 typedef:

类型 定义
std::osyncstream std::basic_osyncstream<char>
std::wosyncstream std::basic_osyncstream<wchar_t>

成员类型

成员类型 定义
char_type CharT[编辑]
traits_type TraitsTraits::char_type 不是 CharT 时程序非良构。[编辑]
int_type Traits::int_type[编辑]
pos_type Traits::pos_type[编辑]
off_type Traits::off_type[编辑]
allocator_type Allocator
streambuf_type std::basic_streambuf<CharT, Traits>
syncbuf_type std::basic_syncbuf<CharT, Traits, Allocator>

成员函数

构造 basic_osyncstream 对象
(公开成员函数) [编辑]
赋值 basic_osyncstream 对象
(公开成员函数) [编辑]
销毁 basic_osyncstream 并发出它的内部缓冲区
(公开成员函数) [编辑]
获得指向底层 basic_syncbuf 的指针
(公开成员函数) [编辑]
获得指向最终目标流缓冲的指针
(公开成员函数) [编辑]
调用底层 basic_syncbuf 上的 emit() 以传输它的内部数据到最终目标
(公开成员函数) [编辑]

继承自 std::basic_ostream

成员函数

有格式输出
插入带格式数据
(std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑]
无格式输出
插入字符
(std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑]
按区块插入字符
(std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑]
寻位
返回输出位置指示器
(std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑]
设置输出位置指示器
(std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑]
杂项
与底层存储设备同步
(std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑]

成员类

为输出操作实现流准备的基本逻辑
(std::basic_ostream<CharT,Traits> 的公开成员类) [编辑]

继承自 std::basic_ios

成员类型

成员类型 定义
char_type CharT
traits_type Traits
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type

成员函数

状态函数
检查是否没有发生错误,即是否可执行输入/输出操作
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
检查是否到达了文件末尾
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
检查是否发生了可恢复的错误
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
检查是否已发生不可恢复的错误
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
检查是否有错误发生(fail() 的同义词)
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
检查是否没有发生错误(!std::basic_ios::fail 的同义词)
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
返回状态标志
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
设置状态标志
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
修改状态标志
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
格式化
复制格式化信息
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
管理填充字符
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
杂项
管理异常掩码
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
设置本地环境
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
管理相关的流缓冲区
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
管理绑定的流
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
窄化字符
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]
拓宽字符
(std::basic_ios<CharT,Traits> 的公开成员函数) [编辑]

注解

功能特性测试 标准 功能特性
__cpp_lib_syncbuf 201803L (C++20) 带同步的缓冲 ostream(std::osyncstreamstd::syncbuf)和操纵符