从 C++ 单例 pthread_once 到 double-check、volatile、memory barrier

本文探讨了C++中使用pthread_once实现单例时可能遇到的问题,包括double-check中的潜在风险,以及内存和缓存问题。引用的文章深入分析了指令乱序和缓存行带来的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pthread_once 确保一个被注册的子函数至多被执行一次:

#include <pthread.h>
#include <cassert>

using namespace std;

class Singleton
{
public:
  static
    Singleton *instance();
private:
  static
    Singleton *instance_;
  static
    pthread_once_t once;
  static
    void _instance();
  Singleton();
};

Singleton::Singleton()
{
};

Singleton *Singleton::instance_ = NULL;

void Singleton::_instance()
{
  assert(NULL == instance_);
  instance_ = new Singleton();
}

pthread_once_t Singleton::once = PTHREAD_ONCE_INIT;

Singleton *Singleton::instance()
{
  pthread_once(&once, Singleton::_instance);
  return instance_;
}


下面这段代码,看起来非常和谐的 double-check,what problem?

pthread_mutex_t singleton_mutex = PTHREAD_MUTEX_INITIALIZER;

Singleton *Singleton::instance2()
{
  if (NULL == instance_) {
    pthread_mutex_lock(&singleton_mutex);
    if (NULL == instance_) {
      instance_ = new Singleton();
    }
    pthread_mutex_unlock(&singleton_mutex);
  }
  return instance_;
}

悲剧在于,由于实现机制的原因,可能存在这一种情况,instance_ 指针先被赋值,而后执行 initializer,在这个初始化完成之前,对于另一个线程而言,这个指针看起来指向一个被初始化过的对象,错误地使用了这个变量!


参考这篇文章: 原子,锁,还有内存屏障 主要讨论指令乱序导致的问题;而这篇:Linux 内核中的内存屏障 讨论的是 cache line 导致的问题。


TO BE CONTINUED......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值