using std::cout in multiple threads

uchar picture uchar · Aug 16, 2013 · Viewed 26.5k times · Source

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

class Printer
{
public:
    void exec()
    {
        mutex m;
        m.lock();
        cout<<"Hello  "<<this_thread::get_id()<<endl;
        chrono::milliseconds duration( 100 );
        this_thread::sleep_for( duration );
        m.unlock();

    }
};

int main()
{
    Printer printer;

    thread firstThread([&printer](){
        while(1)
            printer.exec();

    });
    thread secondThread([&printer](){
        while(1)
            printer.exec();
    });

    firstThread.join();
    secondThread.join();     
}

some of the results :

Hello 11376
Hello 16076
Hello 16076
Hello Hello 11376
16076
Hello 11376
,....

I used mutex for locking threads so I cant understand why two threads are executing std::cout at the same time. It seams very weired to me.Can any one explain what is happening!?!

Answer

hmjd picture hmjd · Aug 16, 2013

The threads are using different mutex instances as the mutex is a local variable in the exec() function so locking the mutex is pointless as each thread will be locking its own mutex resulting in no synchronization between the threads. The same mutex instance must be used by the threads to achieve synchronization.

To correct in the posted code, make the mutex a member variable. However, if another Printer object was created then there would be no synchronization between threads that used different Printer instances. In this case, the mutex would need to be a static member variable to ensure synchronization:

class Printer
{
public:
    //...
private:
    static std::mutex mtx_;
};

std::mutex Printer::mtx_;

To ensure a mutex is always released, regardless if a function exits normally or via an exception, use std:lock_guard:

std::lock_guard<std::mutex> lock(m); // 'm' locked, and will be
                                     // unlocked when 'lock' is destroyed.
std::cout<< "Hello  " << std::this_thread::get_id() << std::endl;
std::chrono::milliseconds duration( 100 );
std::this_thread::sleep_for( duration );