3

This setup

void run()
{
    while (true)
    {
        std::cout << "Hello, Thread!\n";
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

void foo()
{
    std::thread t(run);
    t.detach();
    //std::this_thread::sleep_for(std::chrono::seconds(3));
}

int main()
{
    foo();
    getchar();
}

Gives me no output until I press enter (getchar returns and program ends, but I can see the output for a short while). However, when use the out commented line from foo, the output is shown directly. (Even after foo returns.) I'm using the VS11 beta version. Which behavior is required here according to the standard?

cooky451
  • 3,460
  • 1
  • 21
  • 39
  • Is the output buffered for each thread maybe? – dreamlax Mar 13 '12 at 21:19
  • 1
    Hm.. I tried std::endl, got an assert, switched the lines so that the new thread first waits one second and then makes the output, and hey, it works. Funny. So the output stream couldn't be initialized fast enough or what is going on there? – cooky451 Mar 13 '12 at 21:22
  • 1
    I think writing to standard output and reading from standard input are serialized operations. – R. Martinho Fernandes Mar 13 '12 at 21:22
  • 1
    @R.MartinhoFernandes I don't really know what that means. Any links? :) – cooky451 Mar 13 '12 at 21:25
  • You could try `std::sync_with_stdio(false);`... – Kerrek SB Mar 13 '12 at 21:40
  • http://stackoverflow.com/questions/6374264/is-cout-synchronized-thread-safe - The answer here says, that streams are thread safe. I think, that @R.MartinhoFernandes meant this. – Rafał Rawicki Mar 13 '12 at 21:53
  • I've tested this on GNU/Linux and works perfectly. It outputs "Hello, Thread!" once every second. Are you using windows? – mfontanini Mar 14 '12 at 00:58

3 Answers3

4

"\n" doesn't flush the output buffer. You need to use std::endl instead which flushes the buffer.

void run()
{
    while (true)
    {
        std::cout << "Hello, Thread!"<<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}
Hossein
  • 24,202
  • 35
  • 119
  • 224
3

You could try using std::flush to manually flush the cout buffer

jrl
  • 101
  • 6
-5

I think this is because you are using std::cout, whick is buffered. Try with std::cerr.

D_E
  • 1,196
  • 11
  • 24
  • This is not the reason. For example this code works with GCC under Linux. std::cout buffer is flushed at a newline character. – Rafał Rawicki Mar 13 '12 at 23:08
  • 1
    @Rafał Rawicki: I don't think your statement about the newline character is true. std::cout is flushed if you use std::endl, not if you just use the \n character. Compare http://stackoverflow.com/questions/213907/c-stdendl-vs-n – AbuBakr Mar 27 '12 at 16:58
  • In this context this statement is true, but flushing at the newline is not a general rule, see http://stackoverflow.com/questions/5229096/does-printf-always-flush-the-buffer-on-encountering-a-newline – Rafał Rawicki Mar 27 '12 at 17:14
  • 1
    It's true that `cout` is buffered, but replacing it with `cerr` is a poor "solution" because it's not the same stream, and `stderr` could be redirected elsewhere or closed. A better solution is just to flush `cout`. – Jonathan Wakely Jul 30 '13 at 08:36