相关疑难解决方法(0)

编译时-pthread和-lpthread之间的差异

是什么区别gcc -pthreadgcc -lpthread它在编译多线程程序中使用?

multithreading gcc pthreads thread-safety compiler-flags

99
推荐指数
3
解决办法
5万
查看次数

std :: system实例化单例对象时的异常

我正在学习如何在以后实现线程安全的单例模式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;
}
Run Code Online (Sandbox Code Playgroud)

代码编译很好,但在执行时我收到以下异常.

terminate called after throwing an instance of 'std::system_error'
what():  Unknown error -1 …
Run Code Online (Sandbox Code Playgroud)

c++ singleton c++11

2
推荐指数
1
解决办法
231
查看次数