C++11多线程学习之线程同步

         在C++11中加入了<thread>文件,使得我们编写多线程程序更加方便。        

         既然存在多线程,就会存在线程同步的问题,即多个线程同时使用某一个资源(但是这个资源其实是不能同时使用的)就好像我们上厕所(假定只有一个坑,上大的),只能一个一个人上,而不能大家一起上。看下面这个多线程例子:

#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
 #include <mutex>

//volatile:声明的类型变量表示可以被某些编译器未知的因素更改
volatile int count(0);

void dosomething()
{
	std::cout<<std::this_thread::get_id()<<"占用厕所中..."<<std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));
	std::cout<<std::this_thread::get_id()<<"使用完毕。"<<std::endl;
}

int main() 
{
	std::thread th1(dosomething);
	std::thread th2(dosomething);

	th1.join();
	th2.join();

	system("pause");
	return 0;
}

运行上述程序得到结果:


你会发现两个线程可能同时在使用线程(当然不是指同时间点,是指线程1进去后,线程2又进去了),最后又同时释放线程。

所以我们需要对这两个线程进行同步,这时候就需要用到mutex类了,mutex是一个互斥类。加入mutex后,我们就能给需要的操作加上锁,在一个线程没有运行完之前,不允许其他线程再度访问。

代码实现如下:

#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
 #include <mutex>

//volatile:声明的类型变量表示可以被某些编译器未知的因素更改
volatile int count(0);
std::mutex mtx;

void dosomething()
{
	//加入锁机制,保证线程同步
	mtx.lock();
	std::cout<<std::this_thread::get_id()<<"占用厕所中..."<<std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));
	std::cout<<std::this_thread::get_id()<<"使用完毕。"<<std::endl;
	mtx.unlock();
}

int main() 
{
	std::thread th1(dosomething);
	std::thread th2(dosomething);

	th1.join();
	th2.join();

	system("pause");
	return 0;
}

得到的运行结果:


这时候我们会发现这两个子线程就实现同步了。

除了采用Mutex进行lock()和unlock()方法,我们也还可以采用lock_guard 模板类进行自动解锁,实现线程同步。

实现代码如下:

#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
 #include <mutex>

//volatile:声明的类型变量表示可以被某些编译器未知的因素更改
volatile int count(0);
std::mutex mtx;

void dosomething()
{
	//加入锁机制,保证线程同步
	mtx.lock();
	//使用mtx构造一个lock_guard对象,其中参数adopt_lock表明当前线程已经上锁。
        //此后 mtx 对象的解锁操作交由 lock_guard 对象 lck 来管理,在 lck 的生命周期结束之后,mtx 对象会自动解锁。
	std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);
	std::cout<<std::this_thread::get_id()<<"占用厕所中..."<<std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));
	std::cout<<std::this_thread::get_id()<<"使用完毕。"<<std::endl;
}

int main() 
{
	std::thread th1(dosomething);
	std::thread th2(dosomething);

	th1.join();
	th2.join();

	system("pause");
	return 0;
}
运行结果:



在 dosomething()函数 中,我们首先对 mtx 进行上锁操作(mtx.lock();),然后用 mtx 对象构造一个 lock_guard 对象(std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);),注意此时 Tag 参数为 std::adopt_lock,表明当前线程已经获得了锁,此后 mtx 对象的解锁操作交由 lock_guard 对象 lck 来管理,在 lck 的生命周期结束之后,mtx 对象会自动解锁。

lock_guard 最大的特点就是安全易于使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值