开发者

Why to turn off the standard output buffer when multithreading?

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 s开发者_如何学运维ame if I remove that line on them!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜