std::system 实例化单一实例对象时的异常
std::system Exception when instantiating a singleton object
我正在学习如何在c++11
及更高版本中实现线程安全的单例模式。
#include <iostream>
#include <memory>
#include <mutex>
class Singleton
{
public:
static Singleton& get_instance();
void print();
private:
static std::unique_ptr<Singleton> m_instance;
static std::once_flag m_onceFlag;
Singleton(){};
Singleton(const Singleton& src);
Singleton& operator=(const Singleton& rhs);
};
std::unique_ptr<Singleton> Singleton::m_instance = nullptr;
std::once_flag Singleton::m_onceFlag;
Singleton& Singleton::get_instance(){
std::call_once(m_onceFlag, [](){m_instance.reset(new Singleton());});
return *m_instance.get();
};
void Singleton::print(){
std::cout << "Something" << std::endl;
}
int main(int argc, char const *argv[])
{
Singleton::get_instance().print();
return 0;
}
代码编译良好,但在执行时我收到以下异常。
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted
我尝试用gdb
调试程序。调用std::call_once
时似乎会抛出异常。我不确定发生了什么,但我假设 lambda 表达式无法创建对象。
第二个问题。有没有办法知道未知错误代码的实际含义?我认为在尝试确定问题时,-1
不会有太大帮助。
感谢您的帮助。
发生这种情况是因为您没有使用 -pthread
标志进行编译,并且尝试使用系统上本机线程库中的实用程序。
作为替代方法,请查看示例中对单例模式定义的以下更改,称为"Meyers 单例">
Singleton& Singleton::get_instance(){
static Singleton instance;
return instance;
}
这是线程安全的,并且将导致instance
变量仅初始化一次。 这篇维基百科文章很好地解释了它在引擎盖下如何 https://en.wikipedia.org/wiki/Double-checked_locking 工作。 最好让编译器尽可能为您安排代码。 同样如评论中所述,这个问题也有关于上述的有用信息 迈耶斯对单例模式线程的实现是否安全?
相关文章:
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 对象初始化后在C++中显示 char 数组时的异常行为
- 如何捕获 C++ 内置异常对象
- 异常对象的最后一个潜在销毁点
- 异常表达式创建的异常对象的类型
- 异常对象的静态类型
- 抛出非异常对象
- 异常对象寿命
- 关于异常对象的双重销毁需要解释
- 异常对象在哪里有其空间、堆或堆栈,以及如何在不同的类中访问它
- 如果引发的异常始终是异常对象的副本,为什么不调用此复制构造函数?
- 通过引用捕获(使用 catch)时异常对象的范围
- 存储在异常对象中的信息:在异常中提供字符串
- 当一个异常对象被销毁时(它可以被控制吗)
- 如果我跳出带有"goto"的捕获块,我能保证异常对象将被释放吗?
- 通过引用抛出异常对象是否合适?
- 可以rethrow_exception真正抛出相同的异常对象,而不是副本
- c++能消除异常对象类型的歧义吗?
- 作为抛出异常对象的结构基类