#ifndef SINGLETON_H
#define SINGLETON_H
#include <boost/smart_ptr.hpp>
template< class T >
class singleton
{
public:
typedef boost::shared_ptr< T > T_sptr;
static T_sptr instance()
{
static T_sptr _instance;
if (_instance.get() == 0)
{
_instance.reset(new T);
}
return _instance;
}
protected:
singleton()
{
}
};
#endif // SINGLETON_H
本文介绍了一种使用 C++ 和 Boost 库实现线程安全单例模式的方法。通过模板类和 shared_ptr 进行对象管理,确保了单例实例的唯一性和正确销毁。
6980

被折叠的 条评论
为什么被折叠?



