是什么区别gcc -pthread和gcc -lpthread它在编译多线程程序中使用?
我正在学习如何在以后实现线程安全的单例模式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)