C++11多线程03

首先纠正以前的错误:在没有调用join()之前,线程已经运行了。

#include <iostream>
#include <thread>
#include <mutex>

void shared_print(std::string name , int num)
{
    std::cout<<name<<":"<<num<<std::endl ;
}

void thread_fun()
{
    for(int i = 0 ; i>-100 ;i--)
    {
        shared_print("子线程",i);
    }
}

int main()
{
    std::thread t(thread_fun);

    for(int i=0 ;i<100;i++)
    {
        shared_print("主线程",i);
    }

//    t.join();

    return 0;
}

 

 

下面还是把join加上,目前这个程序输出很乱

#include <iostream>
#include <thread>
#include <mutex>

void shared_print(std::string name , int num)
{
    std::cout<<name<<":"<<num<<std::endl ;
}

void thread_fun()
{
    for(int i = 0 ; i>-100 ;i--)
    {
        shared_print("子线程",i);
    }
}

int main()
{
    std::thread t(thread_fun);

    for(int i=0 ;i<100;i++)
    {
        shared_print("主线程",i);
    }

    t.join();

    return 0;
}

 

使用基本的互斥锁,解决资源竞争,看到输出不在凌乱

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mu ;
void shared_print(std::string name , int num)
{
    mu.lock();
    std::cout<<name<<":"<<num<<std::endl ; //缺点:如果某个线程在这里崩溃,就永远不能unlock了
    mu.unlock();
}

void thread_fun()
{
    for(int i = 0 ; i>-100 ;i--)
    {
        shared_print("子线程",i);
    }
}

int main()
{
    std::thread t(thread_fun);

    for(int i=0 ;i<100;i++)
    {
        shared_print("主线程",i);
    }

    t.join();

    return 0;
}

 

改进:自动释放mu

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mu ;
void shared_print(std::string name , int num)
{
    //优点:guard析构时,会自动释放mu,不怕下面那句异常
    //缺点:cout是全局对象,没有完全的保护起来,其他地方在加锁情况下仍然可以使用
    std::lock_guard<std::mutex> guard(mu);
    std::cout<<name<<":"<<num<<std::endl ;

}

void thread_fun()
{
    for(int i = 0 ; i>-100 ;i--)
    {
        shared_print("子线程",i);
    }
}

int main()
{
    std::thread t(thread_fun);

    for(int i=0 ;i<100;i++)
    {
        shared_print("主线程",i);
    }

    t.join();

    return 0;
}

 

 

继续优化:去掉上一个程序的缺点

#include <iostream>
#include <thread>
#include <mutex>
#include <fstream>

class LofFile
{
public:
    LofFile()
    {
        f.open("log.txt");
    }
    void shared_print(std::string name , int num)
    {
        std::lock_guard<std::mutex> locker(m_mutex) ;
        f<<name<<": "<<num<<std::endl;
    }
private:
    std::mutex m_mutex ;
    std::ofstream f ;
};

void thread_fun(LofFile& log)
{
    for(int i = 0 ; i>-100 ;i--)
    {
        log.shared_print("子线程",i);
    }
}

int main()
{
    LofFile log ;

    std::thread t(thread_fun,std::ref(log));

    for(int i=0 ;i<100;i++)
    {
        log.shared_print("主线程",i);
    }

    t.join();

    return 0;
}

 

 

转载于:https://www.cnblogs.com/guozhikai/p/6105889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值