#include <iostream>
using namespace std;
//懒汉式
class Singelton
{
private:
Singelton()
{
m_singer = NULL;
m_count = 0;
cout << "构造函数Singelton ... do" << endl;
}
public:
static Singelton *getInstance()
{
if (m_singer == NULL ) //懒汉式:1 每次获取实例都要判断 2 多线程会有问题
{
m_singer = new Singelton;
}
return m_singer;
}
static void printT()
{
cout << "m_count: " << m_count << endl;
}
private:
static Singelton *m_singer;
static int m_count;
};
Singelton *Singelton::m_singer = NULL; //懒汉式 并没有创建单例对象
int Singelton::m_count = 0;
void main()
{
cout << "演示 懒汉式" << endl;
Singelton *p1 = Singelton::getInstance(); //只有在使用的时候,才去创建对象。
Singelton *p2 = Singelton::getInstance();
if (p1 != p2)
{
cout << "不是同一个对象" << endl;
}
else
{
cout << "是同一个对象" << endl;
}
p1->printT();
p2->printT();
system("pause");
return ;
}
class CSingleton
{
private:
static CSingleton* m_pInstance;
CSingleton() //构造函数为private
{
}
CSingleton& operator = (const CSingleton& t);
CSingleton(const CSingleton &);
public:
static CSingleton* getInstance()
{
return m_pInstance;
}
};
CSingleton* CSingleton::m_pInstance = new CSingleton;
#include "stdafx.h"
#include "windows.h"
#include "winbase.h"
#include <process.h>
#include "iostream"
using namespace std;
class Singelton
{
private:
Singelton()
{
count ++;
cout<<"Singelton构造函数begin\n"<<endl;
Sleep(1000);
cout<<"Singelton构造函数end\n"<<endl;
}
private:
//防止拷贝构造和赋值操作
Singelton(const Singelton &obj);
Singelton& operator=(const Singelton &obj);
public:
static Singelton *getSingelton()
{
//1"懒汉"模式虽然有优点,但是每次调用GetInstance()静态方法时,必须判断
// NULL == m_instance,使程序相对开销增大。
//2多线程中会导致多个实例的产生,从而导致运行代码不正确以及内存的泄露。
//3提供释放资源的函数
return single;
}
static Singelton *releaseSingelton()
{
if (single != NULL) //需要判断
{
cout<<"释放资源\n"<<endl;
delete single;
single = NULL;
}
return single;
}
void pirntS() //测试函数
{
printf("Singelton printS test count:%d \n", count);
}
private:
static Singelton *single;
static int count;
};
//note 静态变量类外初始化
Singelton *Singelton::single = new Singelton();
int Singelton::count = 0;
int _tmainTTT(int argc, _TCHAR* argv[])
{
Singelton *s1 = Singelton::getSingelton();
Singelton *s2 = Singelton::getSingelton();
if (s1 == s2)
{
cout<<"ok....equal"<<endl;
}
else
{
cout<<"not.equal"<<endl;
}
s1->pirntS();
Singelton::releaseSingelton();
cout <<"hello...."<<endl;
system("pause");
return 0;
}
unsigned int threadfunc2(void *myIpAdd)
{
int id = GetCurrentThreadId();
printf("\n threadfunc%d \n", id);
return 1;
}
void threadfunc(void *myIpAdd)
{
int id = GetCurrentThreadId();
printf("\n threadfunc%d \n", id);
Singelton::getSingelton()->pirntS();
return ;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
DWORD dwThreadId[201], dwThrdParam = 1;
HANDLE hThread[201];
int threadnum = 3;
for (i=0; i<threadnum; i++)
{
//hThread[i] = (HANDLE)_beginthreadex( NULL, 0, &threadfunc, NULL, 0,&dwThreadId[i] );
hThread[i] = (HANDLE)_beginthread(&threadfunc, 0 , 0 );
if (hThread[i] == NULL)
{
printf("begin thread %d error!!!\n", i);
break;
}
}
//等待所有的子线程都运行完毕后,才执行 这个代码
for (i=0; i<threadnum; i++)
{
WaitForSingleObject( hThread[i], INFINITE );
}
printf("等待线程结束\n");
for (i=0; i<threadnum; i++)
{
//CloseHandle( hThread[i] );
}
Singelton::releaseSingelton();
cout <<"hello...."<<endl;
system("pause");
return 0;
}
最后提供一个模板类:
#include <iostream>
#include <string>
#include "CSingleton.h"
using namespace std;
class Test
{
friend class CSingleton<Test> ; //声明Test的友元为单例类模板
private:
string mstr;
Test(): mstr("abc")
{
}
Test& operator = (const Test& t);
Test(const Test&);
public:
void Setmstr(string t)
{
mstr=t;
}
void print()
{
cout<<"mstr = "<<mstr<<endl;
cout<<"this = "<<this<<endl;
}
};
int main()
{
Test *pt1 = CSingleton<Test>::getInstance();
Test *pt2 = CSingleton<Test>::getInstance();
pt1->print();
pt2->print();
pt1->Setmstr("ABCDEFG");
pt2->print();
return 0;
}