c++11库 <thread>的基本使用

创建线程:

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫钰
    > Mail: zhuziyu1157817544@gmail.com
    > Created Time: 2017年12月19日 星期二 13时32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
using namespace std;
void fun()
{
    cout << "This is thread of " << this_thread::get_id()<<  endl;

}
void fun2(int a)
{
    cout << "This is a thread of " << this_thread::get_id() <<"with refenerce " << a << endl;
}
int main()
{
    thread t1(fun);
    t1.join();//子线程执行完主线程才会执行

    thread t2(fun2,5);
    t2.join();

    cout << "father end" <<endl;

}

运行结果:
这里写图片描述

分离子线程

这样原来的线程不用等它了

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫钰
    > Mail: zhuziyu1157817544@gmail.com
    > Created Time: 2017年12月19日 星期二 13时32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
using namespace std;
void fun()
{
    cout << "This is thread of " << this_thread::get_id()<<  endl;

}
void fun2(int a)
{
    cout << "This is a thread of " << this_thread::get_id() <<"with refenerce " << a << endl;
}
int main()
{
    thread t1(fun);
    t1.detah();

    thread t2(fun2,5);
    t2.detah();

    cout << "father end" <<endl;

}

运行结果:
这里写图片描述

因为此时的线程没有确定的顺序,因此显示结果比较乱

加上互斥量

互斥锁对于公共访问区有巨大作用。线程共用一份代码,而没有确定执行顺序的情况下,必须对公共访问区加锁以保证数据不会乱。
先看没有加锁的情况

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫钰
    > Mail: zhuziyu1157817544@gmail.com
    > Created Time: 2017年12月19日 星期二 13时32分14秒
 ************************************************************************/

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

using namespace std;
mutex lock;
static int vary = 50;

void fun()
{
    cout << this_thread::get_id() << "  vary =  " << ++vary <<  endl;

}
void fun2()
{
    cout << this_thread::get_id() << "  vary =  " << ++vary << endl;
}
int main()
{
    thread t1(fun);
    thread t2(fun2);
    t1.detach();
    t2.detach();
    cout << "father end" <<endl;

}

运行结果:
这里写图片描述

再看加锁后的使用:

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫钰
    > Mail: zhuziyu1157817544@gmail.com
    > Created Time: 2017年12月19日 星期二 13时32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
#include<mutex>
#include<algorithm>
using namespace std;
mutex m;
static int vary = 50;

void fun()
{
    m.lock();
    ++vary;
    cout << this_thread::get_id() << "  vary =  " << ++vary <<  endl;
     m.unlock();
}
void fun2()
{
    m.lock();
    ++vary;
    cout << this_thread::get_id() << "  vary =  " << ++vary << endl;
     m.unlock();
}
int main()
{
    thread t1(fun);
    thread t2(fun2);
    t1.join();
    t2.join();
    cout << "father end" <<endl;

}

这里写图片描述

锁中lock_guard的使用

lock_guard:更加灵活的锁管理类模板,构造时是否加锁是可选的,在对象析构时如果持有锁会自动释放锁,所有权可以转移。对象生命期内允许手动加锁和释放锁。

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫钰
    > Mail: zhuziyu1157817544@gmail.com
    > Created Time: 2017年12月19日 星期二 13时32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
#include<mutex>
#include<algorithm>
using namespace std;
mutex m;
static int vary = 50;

void fun()
{
    std::lock_guard<std::mutex> lck(m);
    ++vary;
    cout << this_thread::get_id() << "  vary =  " << ++vary <<  endl;
}
void fun2()
{
    std::lock_guard<std::mutex> lck(m);
    ++vary;
    cout << this_thread::get_id() << "  vary =  " << ++vary << endl;

}
int main()
{
    thread t1(fun);
    thread t2(fun2);
    t1.join();
    t2.join();
    cout << "father end" <<endl;

}

因为有封装mutex的意思,即销毁lock_guard变量执行析构函数自动解锁,因此更方便管理。
结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值