C++ std::ios::sync_with_stdio

本文详细解析了C++中iostream与stdio流的同步机制,包括其默认行为、如何通过sync_with_stdio函数控制同步状态,以及同步对于混合使用iostream和stdio操作的影响。尤其强调了在多线程环境下同步的重要性,以及关闭同步后的潜在问题。

一 作用

设置iostream与stdio流的同步(开、关)

二 函数声明

bool sync_with_stdio (bool sync = true);

三 iosream与stdio流的对应关系

C streamiostream
stdincin
wcin
stdoutcout
wcout
stderrcerr
wcerr
clog
wclog

四 说明

Toggles on or off synchronization of all the iostream standard streams with their corresponding standard C streams if it is called before the program performs its first input or output operation.

If called once an input or output operation has occurred, its effects are implementation-defined.

By default, iostream objects and cstdio streams are synchronized (as if this function was called with true as argument).

翻译:如果在程序执行第一次输入或输出前调用,则开或关所有iostream标准流与它们相应的标准C流的同步。如果调用时,已经发生了输入或输出操作,影响将由实现定义。默认情况下,iostream对象和cstdio流是同步的,等效于用true作为参数调用。

c++98

If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the program.

翻译: 如果流是同步的, 程序可以混合使用iostream和stdio,并且保证他们的观察结果与在程序中使用的顺序相同。

c++11

If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the thread.

Concurrently accessing synchronized streams (i.e., streams for which this function returns true) never introduces data races: characters are read/written individually, although with no further guarantees on its order between threads. This may result in interleaved characters between threads unless proper synchronization of entire operations is enforced by the program.

翻译: 如果流是同步的, 程序可以混合使用iostream和stdio,并且保证他们的观察结果与在线程中使用的顺序相同。

并发访问同步的流(例如,这个函数返回true的流)永远不会引入数据竞争,字符都是单独读或写的,尽管不能进一步保证线程之间的顺序。这可能导致线程间的字符交错,除非程序强制对整个操作进行了正确同步。

With stdio synchronization turned off, iostream standard stream objects may operate independently of the standard C streams (although they are not required to), and mixing operations may result in unexpectedly interleaved characters.

翻译: 在关闭stdio同步之后,iostream标准流对象可以独立于标准C流进行操作(尽管没有要求这样做),但混合操作可能会导致意外的交错字符。

五 参考

原文: std::ios_base::sync_with_stdio

默认情况下,C++的输入输出流与C标准库的输入输出函数是同步的,这会造成一定的性能损失。使用`std::ios::sync_with_stdio(0)`可以关闭这种同步,使`iostream`和`stdio`独立管理各自的缓冲区,避免每次`iostream`执行I/O操作时检查和更新`stdio`缓冲区状态,通常能提升性能,加快输入输出速度,提高程序执行效率,特别是在处理大量数据时,其优势更明显 [^1][^2]。 `scanf`是C标准库中的输入函数,它的速度受限于标准I/O的同步机制。当`std::ios::sync_with_stdio`处于默认的同步状态时,`scanf`与C++的`iostream`同步工作,这可能会影响整体的I/O效率。 在未调用`std::ios::sync_with_stdio(0)`时,`scanf`通常比使用默认同步的`iostream`(如`cin`)快,因为`iostream`的同步机制会带来额外开销。但当调用`std::ios::sync_with_stdio(0)`后,`iostream`的I/O效率大幅提升,在某些情况下,`cin`的速度可能接近甚至超过`scanf`,尤其是在处理复杂的数据类型或格式化输入时。 以下是一个简单的代码示例,用于测试两者读取大量整数的速度: ```cpp #include <iostream> #include <cstdio> #include <chrono> const int N = 1000000; // 使用 std::ios::sync_with_stdio(0) 和 cin 读取数据 void testCin() { std::ios::sync_with_stdio(0); std::cin.tie(0); int num; auto start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < N; ++i) { std::cin >> num; } auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); std::cout << "Using cin: " << duration << " ms" << std::endl; } // 使用 scanf 读取数据 void testScanf() { int num; auto start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < N; ++i) { scanf("%d", &num); } auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); std::cout << "Using scanf: " << duration << " ms" << std::endl; } int main() { testCin(); testScanf(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值