Skip to content

單例模式

讓一個 class 只會產生唯一一個 instance。

cpp
class Manager {
public:
  static Manager& get() {
    static Manager inst;
    return inst;
  }

  Manager(Manager const&) = delete;
  void operator=(Manager const&)  = delete;

private:
  Manager() = default;
};

thread-safe?

In C++11, it is thread safe. According to the standard, §6.7 [stmt.dcl] p4:

If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

Changelog

Just observe 👀