Why to turn off the standard output buffer when mu

2019-06-24 05:28发布

问题:

I'm trying to learn on multithreading, and I have a simple question. On most of the examples I find, the standard output buffer is turned off before letting multiple threads to use it with:

setbuf(stdout,NULL);

Why? Codes print the same if I remove that line on them!

回答1:

It is possible that they would not print out the same - when the output is buffered it may not be displayed right away which can change the order in which the lines are output between threads.

Turning off buffering makes sure you know what order the statements were executed.



回答2:

It prevents buffering which means that you have a better sense of when various threads did what. I.e., you are more likely to see writes to stdout as they occur rather than after some amount of data has been written to stdout.

It's also helpful to do when you are piping a console app's output to a UI.