efficient thread-safe singleton in C++

前端 未结 9 1606
醉话见心
醉话见心 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 18:52

    Use pthread_once, which is guaranteed that the initialization function is run once atomically.

    (On Mac OS X it uses a spin lock. Don't know the implementation of other platforms.)

提交回复
热议问题