4

I am trying to add iostream to the legacy code and thus want to sync those two libraries. According to this article, I should use std::ios_base::sync_with_stdio.

Now, I wonder how it is used in practice (examples please), side-effects I should be aware of.

Thx

vehomzzz
  • 42,832
  • 72
  • 186
  • 216

3 Answers3

11

By default the streams are synchronized, it's guaranteed to work by the standard, you don't have to do anything. sync_with_stdio is only here to disable synchronisation if you want to.

From the article you mentioned :

For the predefined streams, it's safe to mix stdio and iostreams. For example, you can safely use stdin and cin in the same program; the C++ Standard guarantees that it will work the way you would naively expect it to.

The only drawback is a potential performance hit (I guess that's why it can be disabled).

KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83
  • 1
    I remember doing some benchmark, and by default `cout` was slower than `printf`, until I disabled `std::ios_base::sync_with_stdio`, when `cout` became faster. Unfortunately I don't have the numbers... – lvella May 23 '12 at 03:35
  • performance hit can be 10x, according to banchmarks here: [Why is reading lines from stdin much slower in C++ than Python?](https://stackoverflow.com/questions/9371238/why-is-reading-lines-from-stdin-much-slower-in-c-than-python?rq=1) – Alexander Malakhov Dec 14 '17 at 12:03
5

As TheSamFrom1984 says, synced is the default so it should not be a problem. However synchronisation is only relevant when the same stream is being operated on by both libraries. This typically occurs when using cin/cout/cerr and stdin/stdout/stderr respectively. However I can see few reasons for needing to use both simultaneously except when reusing legacy code.

When I first started using C++ I found myself doing this because often I knew how to do something using stdio, but did not know how to do it with iostream, but a better approach would be to figure out how to do in in one or the other, but not both.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

This is the declaration of std::ios_base::sync_with_stdio():

static bool sync_with_stdio( bool sync = true );

It sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation.

The standard C++ streams are the following: std::cin, std::cout, std::cerr, std::clog, std::wcin, std::wcout, std::wcerr and std::wclog.

The standard C streams are the following: stdin, stdout, and stderr.

In practice, this means that the synchronized C++ streams are unbuffered, and each I/O operation on a C++ stream is immediately applied to the corresponding C stream's buffer. This makes it possible to freely mix C++ and C I/O.

Also, synchronized C++ streams are guaranteed to be thread-safe (individual characters output from multiple threads may interleave, but no data races occur)

If the synchronization is turned off, the C++ standard streams are allowed to buffer their I/O independently, which may be considerably faster in some cases.

By default, all eight standard C++ streams are synchronized with their respective C streams.

If this function is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.


Example:

#include <iostream>
#include <cstdio>

int main()
{
    std::ios::sync_with_stdio(false);
    std::cout << "a\n";
    std::printf("b\n");
    std::cout << "c\n";
}

Output:

b
a
c

Source: cppreference

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36