C++ 单例实现 迈耶与call_once
c++ singleton implementation Meyer's vs call_once
嗯,我在自己的项目中使用单例。最常用的单例实现可能是 Meyer 的单例实现和使用 std::call_once 或 pthread_once 的实现。它们对于并行计算都是线程安全的
//Meyer's singleton
class MySingleton{
public:
static MySingleton& getInstance(){
static MySingleton instance;
// volatile int dummy{};
return instance;
}
private:
MySingleton()= default;
~MySingleton()= default;
MySingleton(const MySingleton&)= delete;
MySingleton& operator=(const MySingleton&)= delete;
};
//with call_once
class MySingleton{
public:
static MySingleton& getInstance(){
std::call_once(initInstanceFlag, &MySingleton::initSingleton);
// volatile int dummy{};
return *instance;
}
private:
MySingleton()= default;
~MySingleton()= default;
MySingleton(const MySingleton&)= delete;
MySingleton& operator=(const MySingleton&)= delete;
static MySingleton* instance;
static std::once_flag initInstanceFlag;
static void initSingleton(){
instance= new MySingleton;
}
};
MySingleton* MySingleton::instance= nullptr;
std::once_flag MySingleton::initInstanceFlag;
Meyer 使用局部静态变量来确保线程安全并返回实例标识,而后者是通过call_once和 reture 指针来实现的。在我的实验中,迈耶的暗示速度要快一些。但大多数项目使用call_once方法进行最基本的单例暗示,尽管该项目的一些小部分使用了迈耶的暗示。我只是想知道有什么原则可以遵循,这两种不同的暗示有什么利弊?
call_once
版本保留指向实例的指针,并使用new
创建它。由于指针上从未调用过delete
,因此如果需要在实例析构函数中运行合理的代码,此解决方案会带来一些麻烦。除了进程退出处烦人的悬空指针之外,如果实例析构函数只是默认的析构函数,则可以考虑使用它,就像您的示例中一样。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 模板规范获取'Ambiguous call to overloaded function'
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- Incomings Call with Android Sip stack in Embarcadero C++ bui
- 学习 c++,编译错误"no matching function for call"
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- C++线程"Call to non-static member function without an object argument"
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 如何修复以下错误"no match for call to '(std::tr1::shared_ptr<_iobuf*>) (FILE*&)'"
- 替换派生对象向量中的对象"no matching function to call"
- ROS CPP 相当于 Python subprocess.call() 和 shell=True
- (C++)构造函数、默认参数"call of overloaded... ambigous"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"