Why to turn off the standard output buffer when multithreading?

為{幸葍}努か 提交于 2019-12-10 15:46:35

问题


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.



来源:https://stackoverflow.com/questions/4220911/why-to-turn-off-the-standard-output-buffer-when-multithreading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!