using std::cout in multiple threads

前端 未结 4 1910
刺人心
刺人心 2020-12-30 03:58

I write a simple program for testing Thread in c++11 but std::cout doesnt work as I expect.

class Printer
{
public:
    void exec()
    {
               


        
4条回答
  •  星月不相逢
    2020-12-30 04:38

    You may consider a global std::mutex cout_mutex; (somewhere in your namespaces), which is used to protected std::cout output. Make sure you use std::lock (so you cannot forget to unlock the mutex and for exception safety).

提交回复
热议问题