0
点赞
收藏
分享

微信扫一扫

C++单例结合智能指针解决释放

头文件:

#include "singleton.h"

#include <mutex>
#include<iostream>

std::shared_ptr<Singleton> Singleton::s_instance;
Singleton *Singleton::getInstance()
{
static std::mutex s_mutex;
if (s_instance.get() == NULL)
{
s_mutex.lock();
if (s_instance.get() == NULL)
{
s_instance.reset(new Singleton());
}
s_mutex.unlock();
}
return s_instance.get();
}

Singleton::Singleton()
{

}

源文件:

#include "singleton.h"

#include <mutex>
#include<iostream>

std::shared_ptr<Singleton> Singleton::s_instance;
Singleton *Singleton::getInstance()
{
static std::mutex s_mutex;
if (s_instance.get() == NULL)
{
s_mutex.lock();
if (s_instance.get() == NULL)
{
s_instance.reset(new Singleton());
}
s_mutex.unlock();
}
return s_instance.get();
}

Singleton::Singleton()
{

}


举报

相关推荐

C++ 智能指针

C++——智能指针

【C++】智能指针

C++智能指针

C++进阶 智能指针

0 条评论