一 作用
设置iostream与stdio流的同步(开、关)
二 函数声明
bool sync_with_stdio (bool sync = true);
三 iosream与stdio流的对应关系
C stream | iostream |
stdin | cin |
wcin | |
stdout | cout |
wcout | |
stderr | cerr |
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流进行操作(尽管没有要求这样做),但混合操作可能会导致意外的交错字符。
五 参考