1

I tried using unistd.h sleep(x) on Linux and if the stream isn't explicitly flushed, it'd hang up for the defined time and then output all cout statements at once. On other hand, when using Windows.h header file on my Windows OS, it'd actually wait that amount of time and output each cout statement as if they were individually flushed.

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
cout << "Test";
Sleep(2000);
cout << "Test";
}

So, is it implemented or am I wrong about this?

Man
  • 19
  • 1
  • 4
    Neither the POSIX `sleep` nor the Windows `Sleep` functions have anything to do with the `std::cout` stream or its buffer. The just have no knowledge of the C++ streams or buffers, and so can't flush them. – Some programmer dude Feb 14 '21 at 18:01
  • @Someprogrammerdude Then what could be making this difference between them? – Man Feb 14 '21 at 18:03
  • Different implentations of the streams and the buffers? – Some programmer dude Feb 14 '21 at 18:04
  • @Someprogrammerdude This is not entirely true. File streams are handled by the OS in the end and flushing is an operation provided by the OS as is Sleep. So technically this could be implemented. – idmean Feb 14 '21 at 18:10
  • 2
    @idmean But there are *multiple* levels of buffers for all I/O. C++ streams use their own *application* level buffering. The OS should not know about the contents of these application-level buffers. But like I said, it might be an implementation detail that the Microsoft C++ standard library keeps a timer and flushes the buffers after a while, or uses hooks into some OS-calls to flush them. – Some programmer dude Feb 14 '21 at 18:19
  • @Man What compilers are you using? What versions of them? – Some programmer dude Feb 14 '21 at 18:21
  • @Someprogrammerdude gcc 6.3.0 – Man Feb 14 '21 at 18:25
  • @Man On both Windows and Linux? – Some programmer dude Feb 14 '21 at 18:26

0 Answers0