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

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

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值