指令重排的定义
为了使处理器内部的运算单元能尽量被充分利用,处理器可能会对输入的代码进行乱序执行优化,处理器会在计算之后将乱序执行的结果重组,并确保这一结果和顺序执行结果是一致的,但是这个过程并不保证各个语句计算的先后顺序和输入代码中的顺序一致。这就是指令重排序。
双判断加锁单例模式例子
static Singleton * GetInstance()
{
if (_instance == nullptr)
{
std::lock_guard<std::mutex> lock(_mutex);
if (_instance == nullptr)
{
_instance = new Singleton();
atexit(Destructor);
}
}
return _instance;
}
_instance = new Singleton()虽然是一句代码,但这段代码并不具备原子性,它是由以下三条指令组成的
memory = malloc(); //1.为这个对象分配空间
Singleton(memory); //2.对该空间进行初始化
_instance = memory; //3.设置_instance指向刚分配的内存地址
编译器和处理器可能对这三条指令进行重排,指令重排后为
memory = malloc(); //1.为这个对象分配空间
_instance = memory; //2.设置_instance指向刚分配的内存地址
Singleton(memory); //3.对该空间进行初始化
假设现在有A和B两个线程去执行这单例
首先A线程先执行了指令重排后的前两条指令,_instance就不为nullptr。
此时B线程执行单例时,发现_instance并不为nullptr,不会进入第一个if语句,而是直接返回一个未初始化的Singleton对象的指针,从而导致后续的报错
如何解决
第一种方式:使用C++11中的内存屏障
class Singleton {
public:
static Singleton * GetInstance() {
Singleton* tmp = _instance.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);//获取内存屏障
if (tmp == nullptr) {
std::lock_guard<std::mutex> lock(_mutex);
tmp = _instance.load(std::memory_order_relaxed);
if (tmp == nullptr) {
tmp = new Singleton;
std::atomic_thread_fence(std::memory_order_release);//释放内存屏障
_instance.store(tmp, std::memory_order_relaxed);
atexit(Destructor);
}
}
return tmp;
}
private:
static void Destructor() {
Singleton* tmp = _instance.load(std::memory_order_relaxed);
if (nullptr != tmp) {
delete tmp;
}
}
Singleton(){}
Singleton(const Singleton&) {}
Singleton& operator=(const Singleton&) {}
static std::atomic<Singleton*> _instance;
static std::mutex _mutex;
};
std::atomic<Singleton*> Singleton::_instance;//静态成员需要初始化
std::mutex Singleton::_mutex; //互斥锁初始化
第二种方式: 利用C++11 中 static 特性:如果当变量在初始化的时候,并发同时进⼊声明语句,并发线程将会阻塞等待初始化结束。
class Singleton
{
public:
~Singleton(){}
static Singleton& GetInstance() {
static Singleton instance;
return instance;
}
private:
Singleton(){}
Singleton(const Singleton&) {}
Singleton& operator=(const Singleton&) {}
};
使用static还具有以下优势:
- 利⽤静态局部变量特性,延迟加载;
- 利⽤静态局部变量特性,系统⾃动回收内存,⾃动调⽤析构函数;
- 静态局部变量初始化时,没有 new 操作带来的cpu指令reorder操作;
- c++11 静态局部变量初始化时,具备线程安全;