English 中文(简体)
C++ 高效校对安全
原标题:efficient thread-safe singleton in C++

单一州阶级的通常模式类似。

static Foo &getInst()
{
  static Foo *inst = NULL;
  if(inst == NULL)
    inst = new Foo(...);
  return *inst;    
}

然而,我的理解是,这一解决办法并非read。 (1) 可能不止一次地叫Foo 构造者(可能或可能无关紧要),在Foo 构造返回不同的深层之前,可能无法完全建造。

一种解决办法是,围绕整个方法进行权衡,但在我实际需要很长时间后,我支付同步管理费。 另一种选择类似

static Foo &getInst()
{
  static Foo *inst = NULL;
  if(inst == NULL)
  {
    pthread_mutex_lock(&mutex);
    if(inst == NULL)
      inst = new Foo(...);
    pthread_mutex_unlock(&mutex);
  }
  return *inst;    
}

我是否知道这样做的正确方式,或是否有任何陷阱? 例如,是否存在任何可能出现的任何静态初始化令问题,即最经常保证是民族解放军首次出现。 什么时候?

最佳回答

你们的解决办法被称作双管齐下锁,你书写方式不好。

http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf”rel=“noreferer”>Meyers/Alexandrescupaper解释了为什么——但该文件也广泛误解。 它在C++ meme开始实行双轨制,但实际结论是,C++的双轨制锁可安全地实施,这只是要求在不远的地方使用记忆屏障。

该文件载有一些假冒编码,表明如何利用记忆障碍,安全地执行《防止、禁止和惩治贩运人口特别是妇女和儿童行为的补充议定书》,因此,你很难纠正执行工作。

问题回答

如果你使用C++11,就是这样做的正确途径:

Foo& getInst()
{
    static Foo inst(...);
    return inst;
}

According to new standard there is no need to care about this problem any more. Object initialization will be made only by one thread, other threads will wait till it complete. Or you can use std::call_once. (more info here)

Herb 关于在2014年CppCon打破双管束的彻底会谈。

下面是在C++11实施的法典一,其依据是:

class Foo {
public:
    static Foo* Instance();
private:
    Foo() {}
    static atomic<Foo*> pinstance;
    static mutex m_;
};

atomic<Foo*> Foo::pinstance { nullptr };
std::mutex Foo::m_;

Foo* Foo::Instance() {
  if(pinstance == nullptr) {
    lock_guard<mutex> lock(m_);
    if(pinstance == nullptr) {
        pinstance = new Foo();
    }
  }
  return pinstance;
}

http://ideone.com/olvK13“http://ideone.com/olvK13>。

http://www. opengroup.org/onlinepubs/007908775/xsh/pthread_once.html”rel=“noreferer”>pthread_once

(c) 澳门 X 它使用一个锁。 不知道其他平台的执行情况。

TTBOMK是没有锁定就这样做的唯一有保障的read切方式,就是在<>之前启动所有你的单一吨位。

你们的替代办法是:,“double-checked locking”

可能存在多种读写的记忆模式,但都无法保证这种模式。

阿加澳新集团的单一实施采用双管齐下的锁定模式进行翻新安全,如果你愿意,你可以参考。

可查阅来源代码

TLS在座吗? https://en.wikipedia.org/wiki/Thread- local_storage#C_and_C++

例如,

static _thread Foo *inst = NULL;
static Foo &getInst()
{
  if(inst == NULL)
    inst = new Foo(...);
  return *inst;    
 }

但我们也需要一种方式来明确删除它。

static void deleteInst() {
   if (!inst) {
     return;
   }
   delete inst;
   inst = NULL;
}

解决办法并不安全,因为声明

inst = new Foo();

汇编者可分为两篇:

Statement1: inst = malloc(sizeof(Foo));
Statement2: inst->Foo();

假设在报表1执行后,会发生一个直截了当的背景转变。 第2版还采用了getInstance()方法。 那么第2线透镜就会发现,中点人并非无效。 因此,第2版将返回一个未被接受的物体,因为第1版尚未要求建筑商。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签