在多线程中使用std :: cout [英] using std::cout in multiple threads

查看:680
本文介绍了在多线程中使用std :: cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的程序来测试线程在c ++ 11,但 std :: cout 不工作,因为我的期望。

  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()
{
打印机打印机;

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

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

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

一些结果:


$ b你好11376
你好16076
你好16076
你好你好11376
16076
你好11376
, ....



我使用mutex锁定线程,所以我不明白为什么两个线程正在执行 std :: cout
它接缝非常weired给我。任何一个解释发生了什么!?

解决方案

mutex 不同 mutex > exec()函数,因此锁定 mutex 是无意义的,因为每个线程将锁定自己的 mutex 导致线程之间不同步。同样 mutex 实例必须由线程使用以实现同步。



要更正发布的代码,make mutex 一个成员变量。但是,如果创建了另一个 Printer 对象,则在使用不同的 Printer 实例的线程之间不会有同步。在这种情况下, mutex 需要是一个 static 成员变量以确保同步:

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

std :: mutex Printer :: mtx_;






要确保互斥始终会释放,无论函数是否正常退出或通过异常退出,请使用 std:lock_guard

  std :: lock_guard< std ::互斥>锁(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);


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!?!

解决方案

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 );

这篇关于在多线程中使用std :: cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆