一、C++中使用互斥量
C++标准库为互斥量提供了一个RAII语法的模板类std::lack_guard,其会在构造的时候提供已锁的互斥量,并在析构的时候进行解锁,从而保证了一个已锁的互斥量总是会被正确的解锁
使用互斥量保护列表
#include <list>
#include <mutex>
#include <algorithm>
std::list<int> some_list; // 1
std::mutex some_mutex; // 2
void add_to_list(int new_value)
{
std::lock_guard<std::mutex> guard(some_mutex); // 3
some_list.push_back(new_value);
}
bool list_contains(int value_to_find)
{
std::lock_guard<std::mutex> guard(some_mutex); // 4
return std::find(some_list.begin(),some_list.end(),value_to_find) != some_list.end();
}
精心组织代码来保护共享数据
无意中传递了保护数据的引用
class some_data
{
int a;
std::string b;
public:
void do_something();
};
class data_wrapper
{
private:
some_data data;
std::mutex m;
public:
template<typename Function>
void process_data(Function func)
{
std::lock_guard<std::mutex> l(m);
func(data); // 1 传递“保护”数据给用户函数
}
};
some_data* unprotected;
void malicIoUs_function(some_data& protected_data)
{
unprotected=&protected_data;
}
data_wrapper x;
void foo()
{
x.process_data(malicIoUs_function); // 2 传递一个恶意函数
unprotected->do_something(); // 3 在无保护的情况下访问保护数据
}
这段代码的问题在于,它根本没有做到保护:只是将所有可访问的数据结构代码标记为互斥。函数foo()中调用unprotected->do_something()的代码未能被标记为互斥。
这种情况下,C++线程库无法提供任何帮助,只能由程序员来使用正确的互斥锁来保护数据。
从乐观的角度上看,还是有方法可循的:切勿将受保护数据的指针或引用传递到互斥锁作用域之外,无论是函数返回值,还是存储在外部可见内存,亦或是以参数的形式传递到用户提供的函数中去。
二、解决条件竞争的选项
选项1: 传入一个引用
选项2:无异常抛出的拷贝构造函数或移动构造函数
选项3:返回指向弹出值的指针
下面是一个实例
#include <exception>
#include <memory>
#include <mutex>
#include <stack>
struct empty_stack: std::exception
{
const char* what() const throw() {};
};
template<typename T>
class threadsafe_stack
{
private:
std::stack<T> data;
mutable std::mutex m;
public:
threadsafe_stack(){}
threadsafe_stack(const threadsafe_stack& other)
{
std::lock_guard<std::mutex> lock(other.m);
data = other.data; // 1 在构造函数体中的执行拷贝
}
threadsafe_stack& operator=(const threadsafe_stack&) = delete;
void push(T new_value)
{
std::lock_guard<std::mutex> lock(m);
data.push(new_value);
}
std::shared_ptr<T> pop()
{
std::lock_guard<std::mutex> lock(m);
if(data.empty()) throw empty_stack(); // 在调用pop前,检查栈是否为空
std::shared_ptr<T> const res(std::make_shared<T>(data.top())); // 在修改堆栈前,分配出返回值
data.pop();
return res;
}
void pop(T& value)
{
std::lock_guard<std::mutex> lock(m);
if(data.empty()) throw empty_stack();
value=data.top();
data.pop();
}
bool empty() const
{
std::lock_guard<std::mutex> lock(m);
return data.empty();
}
};
三、死锁
std::lock——可以一次性锁住多个(两个以上)的互斥量,并且没有副作用(死锁风险)。
// 这里的std::lock()需要包含<mutex>头文件
class some_big_object;
void swap(some_big_object& lhs,some_big_object& rhs);
class X
{
private:
some_big_object some_detail;
std::mutex m;
public:
X(some_big_object const& sd):some_detail(sd){}
friend void swap(X& lhs, X& rhs)
{
if(&lhs==&rhs)
return;
std::lock(lhs.m,rhs.m); // 1 std::lock不会自动解锁,需要给每个锁加上std::lock_guard
std::lock_guard<std::mutex> lock_a(lhs.m,std::adopt_lock); // 2
//提供std::adopt_lock参数除了表示std::lock_guard对象已经上锁外,还表示现成的锁,而非尝试创建新的锁
std::lock_guard<std::mutex> lock_b(rhs.m,std::adopt_lock); // 3
swap(lhs.some_detail,rhs.some_detail);
}
};
死锁避免
1.避免嵌套锁,用std::lock锁住多个互斥量
2.避免在持有锁时调用用户提供的代码
3.使用固定顺序获取锁
1.使用层次锁来避免
hierarchical_mutex high_level_mutex(10000); // 1
hierarchical_mutex low_level_mutex(5000); // 2
int do_low_level_stuff();
int low_level_func()
{
std::lock_guard<hierarchical_mutex> lk(low_level_mutex); // 3
return do_low_level_stuff();
}
void high_level_stuff(int some_param);
void high_level_func()
{
std::lock_guard<hierarchical_mutex> lk(high_level_mutex); // 4
high_level_stuff(low_level_func()); // 5
}
void thread_a() // 6
{
high_level_func();
}
hierarchical_mutex other_mutex(100); // 7
void do_other_stuff();
void other_stuff()
{
high_level_func(); // 8
do_other_stuff();
}
void thread_b() // 9
{
std::lock_guard<hierarchical_mutex> lk(other_mutex); // 10
other_stuff();
}
thread_b()运行就不会顺利了,首先,它锁住了other_mutex⑩,这个互斥量的层级值只有100⑦。这就意味着,超低层级的数据(ultra-low-level data)已被保护。当other_stuff()调用high_level_func()⑧时,就违反了层级结构。
2.使用hierarchical_mutex层级互斥量的实现
class hierarchical_mutex
{
std::mutex internal_mutex;
unsigned long const hierarchy_value;
unsigned long prevIoUs_hierarchy_value;
static thread_local unsigned long this_thread_hierarchy_value; // 1 被初始化为最大值
void check_for_hierarchy_violation()
{
if(this_thread_hierarchy_value <= hierarchy_value) // 2 第二个互斥量的层级值必须小于已经持有互斥量检查函数才能通过
{
throw std::logic_error(“mutex hierarchy violated”);
}
}
void update_hierarchy_value()
{
prevIoUs_hierarchy_value=this_thread_hierarchy_value; // 3
this_thread_hierarchy_value=hierarchy_value;
}
public:
explicit hierarchical_mutex(unsigned long value):
hierarchy_value(value),
prevIoUs_hierarchy_value(0)
{}
void lock()
{
check_for_hierarchy_violation();
internal_mutex.lock(); // 4
update_hierarchy_value(); // 5
}
void unlock()
{
this_thread_hierarchy_value=prevIoUs_hierarchy_value; // 6
internal_mutex.unlock();
}
/*try_lock()与lock()的功能相似,除了在调用internal_mutex的try_lock()⑦失败时,不能持有对应锁,所以不必更新层级值,并直接返回false。
虽然是运行时检测,但是它没有时间依赖性——不必去等待那些导致死锁出现的罕见条件。同时,设计过程需要去拆分应用,互斥量在这样的情况下可以消除可能导致死锁的可能性。这样的设计练习很有必要去做一下,即使你之后没有去做,代码也会在运行时进行检查。*/
bool try_lock()
{
check_for_hierarchy_violation();
if(!internal_mutex.try_lock()) // 7
return false;
update_hierarchy_value();
return true;
}
};
thread_local unsigned long
hierarchical_mutex::this_thread_hierarchy_value(ULONG_MAX); // 7
四、std::unique_lock
std::unique_lock和std::lock_guard
std::unique_lock会占用比较多的空间,并且比std::lock_guard稍慢一些。保证灵活性要付出代价,这个代价就是允许std::unique_lock实例不带互斥量:信息已被存储,且已被更新。
1.交换操作中std::lock()和std::unique_lock的使用
class some_big_object;
void swap(some_big_object& lhs,some_big_object& rhs);
class X
{
private:
some_big_object some_detail;
std::mutex m;
public:
X(some_big_object const& sd):some_detail(sd){}
friend void swap(X& lhs, X& rhs)
{
if(&lhs==&rhs)
return;
std::unique_lock<std::mutex> lock_a(lhs.m,std::defer_lock); // 1
std::unique_lock<std::mutex> lock_b(rhs.m,std::defer_lock); // 1 std::def_lock 留下未上锁的互斥量
std::lock(lock_a,lock_b); // 2 互斥量在这里上锁
swap(lhs.some_detail,rhs.some_detail);
}
};
2.unique_lock可以加std::adopt_lock参数
①加上std::try_to_lock
可以避免一些不必要的等待,会判断当前mutex能否被lock,如果不能被lock,可以先去执行其他代码
#include <iostream>
#include <mutex>
std::mutex mlock;
void work1(int& s) {
for (int i = 1; i <= 5000; i++) {
std::unique_lock<std::mutex> munique(mlock, std::try_to_lock);
if (munique.owns_lock() == true) {
s += i;
}
else {
// 执行一些没有共享内存的代码
}
}
}
void work2(int& s) {
for (int i = 5001; i <= 10000; i++) {
std::unique_lock<std::mutex> munique(mlock, std::try_to_lock);
if (munique.owns_lock() == true) {
s += i;
}
else {
// 执行一些没有共享内存的代码
}
}
}
int main()
{
int ans = 0;
std::thread t1(work1, std::ref(ans));
std::thread t2(work2, std::ref(ans));
t1.join();
t2.join();
std::cout << ans << std::endl;
return 0;
}
②加上std::defer_lock
这个参数表示暂时先不lock,之后手动去lock,但是使用之前也是不允许去lock。一般用来搭配unique_lock的成员函数去使用
#include <iostream>
#include <mutex>
std::mutex mlock;
void work1(int& s) {
for (int i = 1; i <= 5000; i++) {
std::unique_lock<std::mutex> munique(mlock, std::defer_lock);
munique.lock();
s += i;
munique.unlock(); // 这里可以不用unlock,可以通过unique_lock的析构函数unlock
}
}
void work2(int& s) {
for (int i = 5001; i <= 10000; i++) {
std::unique_lock<std::mutex> munique(mlock, std::defer_lock);
munique.lock();
s += i;
munique.unlock();
}
}
int main()
{
int ans = 0;
std::thread t1(work1, std::ref(ans));
std::thread t2(work2, std::ref(ans));
t1.join();
t2.join();
std::cout << ans << std::endl;
return 0;
}
还有一个成员函数是try_lock,和上面的try_to_lock参数的作用差不多,判断当前是否能lock,如果不能,先去执行其他的代码并返回false,如果可以,进行加锁并返回true,代码如下:
void work1(int& s) {
for (int i = 1; i <= 5000; i++) {
std::unique_lock<std::mutex> munique(mlock, std::defer_lock);
if (munique.try_lock() == true) {
s += i;
}
else {
// 处理一些没有共享内存的代码
}
}
}
release函数,解除unique_lock和mutex对象的联系,并将原mutex对象的指针返回出来。如果之前的mutex已经加锁,需在后面自己手动unlock解锁,代码如下:
void work1(int& s) {
for (int i = 1; i <= 5000; i++) {
std::unique_lock<std::mutex> munique(mlock); // 这里是自动lock
std::mutex *m = munique.release();
s += i;
m->unlock();
}
}
3.不同域中互斥量所有权的转移
std::unique_lock<std::mutex> get_lock()
{
extern std::mutex some_mutex;
std::unique_lock<std::mutex> lk(some_mutex);
prepare_data();
return lk; // 1
}
void process_data()
{
std::unique_lock<std::mutex> lk(get_lock()); // 2
do_something();
}
五、保护共享数据的替代设施
1.处理条件竞争
C++标准库提供了std::once_flag和std::call_once来处理条件竞争。
比起锁住互斥量,并显式的检查指针,每个线程只需要使用std::call_once,在std::call_once的结束时,就能安全的知道指针已经被其他的线程初始化了。
使用std::call_once比显式使用互斥量消耗的资源更少,特别是当初始化完成后。
std::shared_ptr<some_resource> resource_ptr;
std::once_flag resource_flag; // 1
void init_resource()
{
resource_ptr.reset(new some_resource);
}
void foo()
{
std::call_once(resource_flag,init_resource); // 可以完整的进行一次初始化
resource_ptr->do_something(); //some_resource初始化完成,可以使用resource_ptr
}
2.使用std::call_once作为类成员的延迟初始化(线程安全)
class X
{
private:
connection_info connection_details;
connection_handle connection;
std::once_flag connection_init_flag;
void open_connection()
{
connection=connection_manager.open(connection_details);
}
public:
X(connection_info const& connection_details_):
connection_details(connection_details_)
{}
void send_data(data_packet const& data) // 1
{
std::call_once(connection_init_flag,&X::open_connection,this); // 2
connection.send_data(data);
}
data_packet receive_data() // 3
{
std::call_once(connection_init_flag,&X::open_connection,this); // 2
return connection.receive_data();
}
};
在只需要一个全局实例情况下,这里提供一个std::call_once的替代方案
class my_class;
my_class& get_my_class_instance()
{
static my_class instance; // 线程安全的初始化过程
return instance;
}
多线程可以安全的调用get_my_class_instance()①函数,不用为数据竞争而担心。
在C++11标准中:初始化及定义完全在一个线程中发生,并且没有其他线程可在初始化完成前对其进行处理,条件竞争终止于初始化阶段,这样比在之后再去处理好的多
3.比起使用std::mutex实例进行同步,不如使用boost::shared_mutex来做同步
使用boost::shared_mutex对数据结构进行保护
#include <map>
#include <string>
#include <mutex>
#include <boost/thread/shared_mutex.hpp>
class dns_entry;
class dns_cache
{
std::map<std::string,dns_entry> entries;
mutable boost::shared_mutex entry_mutex;
public:
dns_entry find_entry(std::string const& domain) const
{
boost::shared_lock<boost::shared_mutex> lk(entry_mutex); // 1 保护其共享和只读权限
std::map<std::string,dns_entry>::const_iterator const it=
entries.find(domain);
return (it==entries.end())?dns_entry():it->second;
}
void update_or_add_entry(std::string const& domain,
dns_entry const& dns_details)
{
std::lock_guard<boost::shared_mutex> lk(entry_mutex); // 2 独占访问权限
entries[domain]=dns_details;
}
};
4.嵌套锁
C++标准库提供了std::recursive_mutex类
嵌套锁一般用在可被多线程并发访问的类上,所以其拥有一个互斥量保护其成员数据。每个公共成员函数都会对互斥量上锁,然后完成对应的功能,之后再解锁互斥量