2
#include <iostream>
#include <chrono>
#include <thread>

using namespace std::chrono;


int main() {
    std::cout << "hello";
    std::this_thread::sleep_for(2s);
    std::cout << "world"<<std::endl;
}

I m using visual studio 2019.
I expect above code to print helloworld after 2 seconds but hello is displayed before 2 seconds in the console(i.e instantly when i run the program) and world is displayed after 2 seconds.
I read about buffer recently and came to know that the text should be displayed in the console only after the buffer is full. We can force to flush the buffer by using \n, manupulators and cin.

But why am I not seeing desired behavior in this particular example?
Isn't cout using buffer?
Is it flushed for every character?

Aayush Neupane
  • 1,066
  • 1
  • 12
  • 29
  • 1
    I didn't not reproduce this: https://wandbox.org/permlink/PCKs3MfduGHlVd83 (I added some text before to see the moment when compilation ends, `helloworld` appears together after two seconds). The buffering and flushing is dependent on many things,I don't think you can get a single answer. For what it's worth, `std::cout` does use buffering and it's unlikely that it's flushed after every character (that would be highly inefficient). Perhaps your compiler noticed sleep and decided to flush, or it just flushes after every `<<` operator call in non-optimized builds? – Yksisarvinen Jul 24 '20 at 09:59
  • _i expect above code to print hello before and world after 2 seconds but hello is displayed before 2 seconds in the console_ You wording is confusing. Did you expect the hello _and_ world to appear after the 2 seconds? – Scheff's Cat Jul 24 '20 at 10:00
  • I also could not reproduce. `helloworld` appears together after 2 sec like @Yksisarvinen wrote (g++ 8 on Linux). – Łukasz Ślusarczyk Jul 24 '20 at 10:10
  • 1
    I just tried your MCVE on my side. 1. in cygwin/bash/g++: The behavior was like (you might have) expected: helloworld appeared after 2 s. 2. in Windows Console/cmd.exe/cl (cl of VS2017): hello appeared instantly, world after 2 s. 3. I executed the test.exe (built in 2.) in cygwin/bash again. Now, helloworld appeared at once (after 2 s) like in 1. – Scheff's Cat Jul 24 '20 at 10:11
  • @Scheff i have edited my question to make it more clear. when i run the program hello is displayed instatly and world id displayed only after 2 seconds are passed – Aayush Neupane Jul 26 '20 at 09:06
  • @Yksisarvinen does flushing really depends on sleep? – Aayush Neupane Jul 26 '20 at 09:09
  • @AAYUSHNEUPANE No idea. Optimizations is a vast topic and I just shoot a guess that it's possible. According to Scheff, it works only in MSVC and only when run in true Windows environment (unlike simulated Unix in Cygwin), so it's unlikely that it's a compile time optimization. Might be something about Cygwin or the environment you run your program in (perhaps the environment itself buffers lines, independently of C++ program). – Yksisarvinen Jul 27 '20 at 08:16

1 Answers1

0

There is no strict rule in the standard when a buffer should be flushed.

Like @Yksisarvinen mentioned in the comments, a flush isn't affected by the compiler but by the operating system. Also \n doesn't always trigger a flush. If the cout is going to a terminal, it is usually line buffered, thus you can force a flush via \n.

More information can be found at: When does cout flush? and Does new line character also flush the buffer?

tdoecke
  • 50
  • 1
  • 7