C++ 11 互斥量

C++中通过实例化mutex创建互斥量,通过调用成员函数lock()进行上锁,unlock()进行解锁。不过,不推荐实践中直接去调用成员函数,因为调用成员函数就意味着,必须记住在每个函数出口都要去调用unlock(),也包括异常的情况。C++标准库为互斥量提供了一个RAII语法的模板类,从而保证了一个已锁的互斥量总是会被正确解锁。例子如下:

在多任务操作系统中,同时运行的多个任务可能都需要使用同一资源。例如有一个人正在使用打印机打印东西的同时(还没有打印结束),另一个人刚好也在此刻使用打印机打印东西,如果 不做任何处理的话,打印出来的东西肯定是错乱的。

#include <iostream>
#include <thread>
using namespace std;

void printer(const char *str)
{
	
	while (*str!='\0')
	{
		cout << *str++;
		this_thread::sleep_for(chrono::seconds(1));
	}
	cout << endl;

}

//线程1
void func1()
{
	const char *str = "hello";
	printer(str);
}

//线程2
void func2()
{
	const char *str = "world";
	printer(str);
}

int main()
{
	
	thread t1(func1);
	thread t2(func2);
	t1.join();
	t2.join();

	return 0;
}

运行结果:

独占互斥量 mutex

互斥量的基本接口很相似,一般用法是通过lock()方法来阻塞线程,直至获得互斥量的所有权为止。在线程获得互斥量并完成任务之后,就必须使用unlock()来解除对互斥量的占有,lock()和unlock()必须成对出现。try_lock()尝试锁定互斥量,如果成功则返回true,如果失败则返回false,它是非阻塞的。

#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

mutex g_lock;//全局互斥锁对象,头文件#include <mutex>
void printer(const char *str)
{
	g_lock.lock();//上锁
	while (*str!='\0')
	{
		cout << *str++;
		this_thread::sleep_for(chrono::seconds(1));
	}
	cout << endl;
	g_lock.unlock();//解锁
}

//线程1
void func1()
{
	const char *str = "hello";
	printer(str);
}

//线程2
void func2()
{
	const char *str = "world";
	printer(str);
}

int main()
{
	
	thread t1(func1);
	thread t2(func2);
	t1.join();
	t2.join();

	return 0;
}

运行结果:

使用lock_guard可以简化lock/unlock 的写法,同时也更安全,因为lock_guard在构造时会自动锁住互斥量,而在退出作用域后进行析构时就会自动解锁,从而避免忘了unlock操作。

#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

mutex g_lock;//全局互斥锁对象,头文件#include <mutex>
void printer(const char *str)
{
	//g_lock.lock();//上锁
	lock_guard<mutex> locker(g_lock);//这里lock_guard代替了lock/unlock
	while (*str!='\0')
	{
		cout << *str++;
		this_thread::sleep_for(chrono::seconds(1));
	}
	cout << endl;
	//g_lock.unlock();//解锁
}

//线程1
void func1()
{
	const char *str = "hello";
	printer(str);
}

//线程2
void func2()
{
	const char *str = "world";
	printer(str);
}

int main()
{
	
	thread t1(func1);
	thread t2(func2);
	t1.join();
	t2.join();

	return 0;
}

运行结果:

猜你喜欢

转载自blog.csdn.net/any1234567890/article/details/83413844
今日推荐