#include <iostream>
#include <windows.h>
#include <mutex>
std::mutex gmutex;
using namespace std; template<typename Type>
class Singleton
{
public:
static Type* GetSingleton()
{
if (siglen == NULL)
{
unique_lock<std::mutex> lock(gmutex);//C++11加锁。
if (siglen == NULL)
{
siglen = new Type();
Type *temp = new Type();
MemoryBarrier();
siglen = temp;
}
}
return siglen;
}
private:
static Type* siglen;
}; template<typename Type>
Type* Singleton<Type>::siglen = NULL; class Text
{
public:
Text()
{
data = 100;
//由于是单例模式。所以唯一会出现申请内存。调用构造
//函数。赋值三个步骤混乱的机会仅仅有在前面的1-2次
//的时候。可惜速度太快了。这样的情况发生的概率及其低
//,可是我们的心理要始终明确。 }
void Printf()
{
cout << "data="<<data << endl;
}
static DWORD WINAPI ThreadFunc(LPVOID arg)
{
Singleton<Text>::GetSingleton()->Printf();
return DWORD(0);
}
private:
int data;
}; int main()
{
HANDLE hThread;
DWORD threadId; for (int i = 0; i < 10; i++)
{
hThread = CreateThread(NULL, 0, &(Text::ThreadFunc), (void *)"123",0, &threadId);
}
Sleep(5);
cout << "ThreadFunc is running!!!" << endl;
return 0;
} #include <iostream>
using namespace std;
//引用计数的智能指针。
template<typename Type>
class my_auto_ptr
{
public:
my_auto_ptr(Type* p = NULL) :ptr(p)
{
count = new int[1];
count[0] = 1;
}
my_auto_ptr(const my_auto_ptr &ma)
{
count = ma.count;
count[0]++;
}
my_auto_ptr& operator=(const my_auto_ptr &ma)
{
if (this != &ma)
{
this->~my_auto_ptr();
count = ma.count;
count[0]++;
ptr = ma.ptr;
}
return *this;
}
~my_auto_ptr()
{
if (count!=NULL &&count[0]-- == 1)
{
cout << "~my_auto_ptr()" << endl;
delete ptr;
ptr = NULL;
delete[] count;
count = NULL;
}
}
Type* operator->()
{
return ptr;
}
Type& operator*()
{
return *ptr;
}
private:
Type *ptr;
int *count;
};
int main()
{
my_auto_ptr<int> ps(new int(100));
my_auto_ptr<int> pb(ps);
my_auto_ptr<int> pd;
pd = pb;
return 0;
}

C++再论单例模式的更多相关文章

  1. 05-IOSCore - 单例模式、KVO

    单例模式 是设计模式之一,使用频率高,让数据或对象在程序的各个地方都能访问,保持唯一 要素: 各个地方都能访问方法 + 静态消息 只要导入类 就能访问 保持唯一 1.在静态消息内限制对象的创建 2.外 ...

  2. DCL单例模式

    我们第一次写的单例模式是下面这样的: public class Singleton { private static Singleton instance = null; public static ...

  3. Java项目(5)——单例模式的应用与研究

    单例模式是非常别致的一个模式,非常少有人拿它跟其它模式相比,由于,单例模式非常easy,非常特别,作用就是保证一个类有唯一一个实例,并让一个全局变量使得它能被訪问.而保证这个类仅仅被实例化一次的办法就 ...

  4. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

  5. Spring------概述

    Spring框架------概述: spring是j2ee应用程序框架,是轻量级的IOC和AOP的容器框架,主要是针对JAVABean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts ...

  6. Android Context完全解析

    Context类型 我们知道,Android应用都是使用Java语言来编写的,那么大家可以思考一下,一个Android程序和一个Java程序,他们最大的区别在哪里?划分界限又是什么呢?其实简单点分析, ...

  7. Android各种获取Context方法

    首先讲一讲这四个函数的区别,后面还有我对context的一些理解区别如下所示: 原文链接http://stackoverflow.com/questions/6854265/getapplicatio ...

  8. Java实战之03Spring-01Spring概述

    一.Spring概述 1.Spring是什么? Spring是分层的Java SE/EE应用 full-stack轻量级开源框架,以IoC(Inverse Of Control:反转控制)和AOP(A ...

  9. spring框架的一些技术总结

    纵观现在互联网行业,java的地位依然不可动摇,因为java拥有着的众多开发人员和适用性广,技术与解决技术大部分开源等特点,因此java依然是众多众多开发行业作为互联网开发的首选,而说到开发,我们就不 ...

随机推荐

  1. TCP三次握手及四次挥手详解及常见面试题

    https://blog.csdn.net/ZWE7616175/article/details/80432486

  2. luogu4301 [CQOI2013]新Nim游戏

    nim和线性基 #include <algorithm> #include <iostream> #include <cstdio> using namespace ...

  3. Leetcode 400.第n个数

    第n个数 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 为整形范围内 ( n < 231). 示例 1: 输入: 3 输出 ...

  4. Git x SVN rebase事故

    Git x SVN rebase事故 @author ixenos 2019-01-09 14:21:21 前言: 昨天在Git x SVN 中进行git svn dcommit的时候,提示需要再进行 ...

  5. element-ul InputNumber 空白

    if(this.days == undefined){ this.$nextTick(function(){ this.days = 1; }); }

  6. HDU——2112HDU Today(SPFA+简单Hash或map+前向星)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. LightOJ——1066Gathering Food(BFS)

    1066 - Gathering Food   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB W ...

  8. 【二叉搜索树】poj 1577 Falling Leaves

    http://poj.org/problem?id=1577 [题意] 有一颗二叉搜索树,每次操作都把二叉搜索树的叶子从左到右揪掉(露出来的父节点就变成了新的叶子结点) 先给出了揪掉的叶子序列(多个字 ...

  9. Heritage of skywalkert

    Heritage of skywalkert skywalkert, the new legend of Beihang University ACM-ICPC Team, retired this ...

  10. Lucas 卢卡斯定理

    Lucas: 卢卡斯定理说白了只有一条性质 $$ C^n_m \equiv C^{n/p}_{m/p} \times C^{n \bmod p}_{m \bmod p} \ (mod \ \ p) $ ...