Weird list print function behaviour. Works if i print “\n”, doesn't work if I remove it

后端 未结 3 1902
暖寄归人
暖寄归人 2021-01-27 17:02

Sometimes c++ plays me big time. I really can\'t think of why this does/doesn\'t work and I\'d be happy if any of you knew.

I call this function once every second on a t

相关标签:
3条回答
  • 2021-01-27 17:17

    Switch over to using std::cerr as an experiment and I would bet you that it prints fine every time because standard error is not buffered while cout/printf (standard out) is buffered. Placing a std::endl at the end will buffer flush and put a new line out for cout which should work as well. You could also simply work on standard out and call a flush function on the stream to ensure its printed to console which would be similar.

    0 讨论(0)
  • 2021-01-27 17:38

    I guess that's because STDOUT is buffered, and printing \n flushes the buffer - see SO on printf \n behaviour

    0 讨论(0)
  • This has nothing to do with threading or C++. The OS is buffering your output, and the \n implicitly flushes the buffer when stdout is a console. Call fflush(stdout) after the loop if you want every call to show its output immediately.

    0 讨论(0)
提交回复
热议问题