c++单例模式
来源:互联网 发布:dreamweaver 替代软件 编辑:程序博客网 时间:2024/06/12 01:40
class Parent
{
public:
static Parent* GetInstense()
{
static boost::mutex p_mutex;
static Parent *S_singel;
if (NULL != S_singel)
{
return S_singel;
}
static boost::gurad_lock(p_mutex);//多线程中
if (NULL != S_singel)//当两个以上线程使用时,先获得锁的线程(其他线程在此处锁住)new对象,释放锁后,其他线程得到锁,这时候S_singel直接返回
{
return S_singel;
}
S_singel = new (std::nothrow)Parent;
if (NULL == S_singel)
{
cout << “error: new parent faired” << endl;
}
return S_singel;
}
private:
Parent();//构造函数和析构函数必须私有化,不允许直接new Parent,单例模式就只能通过GetInstense()获取
~Parent();
};
void main()
{
Parent * pParent = Parent::GetInstense();
if (NULL != pParent)
{
//
}
}
- C#单例模式
- C#-单例模式
- 单例模式(C++)
- 【C++】单例模式
- [C++]单例模式
- 单例模式(C++)
- C ++的单例模式
- 【C++】Chapter17:单例模式
- Object-c单例模式
- Objectove-c单例模式
- Objective C--单例模式
- Objective C--单例模式
- Objective C 单例模式
- Objective C--单例模式
- 单例模式(C#)
- 单例模式(C#)
- object-c 单例模式
- c++-单例模式Singleton
- RuntimeException和Exception区别
- 7.5 Javascript:表单验证-正则补充
- 读书笔记 - Windows程序设计(滚动条)
- hdu 2952 Counting Sleep (深度搜索)
- Android 如何获取一个layout实例
- c++单例模式
- Oracle归档日志管理
- 关于Hibernate中的update报org.hibernate.HibernateException: No Session found for current thread
- Android RecyclerView使用简单介绍
- 解决项目开发过程中JS 缓存问题
- Java二分查找
- Android 如何在一个layout中引用另一个layout
- 移动端h5开发相关内容总结--CSS篇
- 学习OPENCV