C++20中头文件syncstream的使用

<syncstream>是C++20中新增加的头文件,提供了对同步输出流的支持,即在多个线程中可安全地进行输出操作,此头文件是Input/Output库的一部分。包括:

1.std::basic_syncbuf:是std::basic_streambuf的包装器(wrapper),它将输出累积在其自己的内部缓冲区中,并在销毁时以及明确请求时将其全部内容自动传输到包装的缓冲区,以便它们显示为连续的字符序列。此类模板主要用于创建与多线程程序无缝协作的自定义流类。如果多个线程试图同时写入同一个流,std::basic_syncbuf将确保每个线程的输出都以正确的顺序写入流,而不会出现任何交错。

cpp 复制代码
using syncbuf  = basic_syncbuf<char>;
using wsyncbuf = basic_syncbuf<wchar_t>;

2.std::basic_osyncstream:是std::basic_syncbuf的便捷包装器。它提供了一种机制来同步写入同一流的线程。

cpp 复制代码
using osyncstream  = basic_osyncstream<char_t>;
using wosyncstream = basic_osyncstream<wchar_t>;

测试代码如下:

cpp 复制代码
namespace {

void func1(int id, std::ostream& sync_out)
{
	std::osyncstream sync_stream(sync_out);
	sync_stream << "thread " << id << " is running" << std::endl;
}

void func2(int id, std::ostream& out)
{
	out << "thread " << id << " is running" << std::endl;
}

void sync(std::ofstream& file_out)
{
	std::syncbuf sync_buf(file_out.rdbuf());
	std::ostream sync_out(&sync_buf);

	constexpr int num_threads{ 10 };
	std::vector<std::thread> threads;

	for (int i = 0; i < num_threads; ++i)
		threads.emplace_back(func1, i, std::ref(sync_out));

	for (auto& t : threads)
		t.join();
}

void common(std::ofstream& file_out)
{
	constexpr int num_threads{ 10 };
	std::vector<std::thread> threads;

	for (int i = 0; i < num_threads; ++i)
		threads.emplace_back(func2, i, std::ref(file_out));

	for (auto& t : threads)
		t.join();
}

} // namespace

int test_syncstream()
{
#ifdef _MSC_VER
	std::ofstream file_out1("../../../testdata/output1.txt");
	std::ofstream file_out2("../../../testdata/output2.txt");
#else
	std::ofstream file_out1("testdata/output1.txt");
	std::ofstream file_out2("testdata/output2.txt");
#endif

	if (!file_out1 || ! file_out2) {
		std::cerr << "fail to open file for writing" << std::endl;
		return -1;
	}

	sync(file_out1);
	common(file_out2);

	file_out1.close();
	file_out2.close();
	return 0;
}

执行结果如下图所示:每次输出并不完全一致:output2.txt中输出是乱的,而output1.txt中的输出是正常的

GitHub :https://github.com/fengbingchun/Messy_Test

相关推荐
繁华的地方不一定留下你的脚印4 天前
C++20 concepts:把模板要求写在入口,而不是等一屏报错
c++20
码匠许师傅4 天前
【C++三方组件】Asio 下篇:C++20 协程版 TCP 客户端与服务端
c++·tcp/ip·c++20
啦啦啦啦啦zzzz21 天前
利用RAII机制进行日志的采集
linux·服务器·c++·网络编程·c++20
啦啦啦啦啦zzzz22 天前
Logger的组装(c++20)
c++·算法·c++20
啦啦啦啦啦zzzz24 天前
Appender抽象类的实现c++20
linux·服务器·c++·c++20
OPEN-F1 个月前
C++20新特性精讲:概念、范围与三路比较
算法·c++20
小小龙学IT1 个月前
C++ std::chrono 时间库深度解析:从 duration 模板到 C++20 日历与时区
linux·c++·c++20
郝学胜-神的一滴1 个月前
C++20 高级编程 004:从初始化、内存到const系列关键字
开发语言·算法·编程·软件构建·c++20
可爱系程序猿1 个月前
msvcp120.dll 加载失败排查:VC++ 2013、进程架构与插件私有目录如何对应
程序人生·电脑·c++20
ShineWinsu1 个月前
对于C++:C++20中线程、初始化、Lambda与内存视图等特性的解析
c++·c++20