efficient thread-safe singleton in C++

前端 未结 9 1612
醉话见心
醉话见心 2020-11-28 18:30

The usual pattern for a singleton class is something like

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


        
9条回答
  •  鱼传尺愫
    2020-11-28 19:06

    Your alternative is called "double-checked locking".

    There could exist multi-threaded memory models in which it works, but POSIX does not guarantee one

提交回复
热议问题