Synchronizing STD cout output multi-thread

前端 未结 5 1181
梦毁少年i
梦毁少年i 2020-12-15 10:21

Latelly I\'ve been working with multi-thread coding, after a while writing I realized that if I used std::cout in different boost::threads, the output would came without a l

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 10:58

    Give each thread a std::ostringstream to write output to. At the end of the program, print each thread's output in order.

    How else would you do it, considering that thread 4 may finish long before thread 1?

提交回复
热议问题