Lazy instance is an object which is created on the first time it’s accessed. And chrome implements a very powerful LazyInstance template class, which is very fast and thread safe. See detail in ( lazy_instance.h ).

Class: LazyInstance<Type, Traits>

The LazyInstance<Type, Traits> class manages a single instance of Type, which will be lazily created on the first time it’s accessed.

LazyInstance is completely thread safe, assuming that you create it safely. The class was designed to be POD initialized, so it shouldn’t require a static constructor. It really only makes sense to declare a LazyInstance as a global variable using the LAZY_INSTANCE_INITIALIZER initializer.

Compare with Singleton


#include "base/at_exit.h"
#include "base/lazy_instance.h"

class MyClass
        // enter constructor for each lazy instance
        m_val = 0;
    void setVal(int n) { m_val = n; }
    int getVal() { return m_val; }

    int m_val;

static base::LazyInstance<MyClass> inst_1 = LAZY_INSTANCE_INITIALIZER;
static base::LazyInstance<MyClass> inst_2 = LAZY_INSTANCE_INITIALIZER;

void LazyInstanceTest()
    // needed for lazy instance
    base::AtExitManager exit_mgr;

    int v1 = inst_1.Pointer()->getVal();

    int v2 = inst_2.Pointer()->getVal();