#include<iostream>
#include<pthread.h>
using namespace std;
class Single
{
public:
static pthread_mutex_t mtx;
static Single* get_Single()
{
if(p==NULL)
{
pthread_mutex_lock(&mtx);
if(p==NULL)
{
p=new Single;
}
pthread_mutex_unlock(&mtx);
}
return p;
}
private:
static Single* volatile p;
Single()
{
}
};
pthread_mutex_t Single ::mtx=PTHREAD_MUTEX_INITIALIZER;
Single* Single::p=NULL;
void* func1(void* arg)
{
while(1)
{
Single* tmp1=Single::get_Single();
cout<<"tmp1 "<<tmp1<<endl;
sleep(1000);
}
}
void* func2(void* arg)
{
while(1)
{
Single* tmp2=Single::get_Single();
cout<<"tmp2 "<<tmp2<<endl;
sleep(1000);
}
}
int main()
{
pthread_t pt1;
pthread_t pt2;
int ret1=pthread_create(&pt1,0,func1,0);
if(ret1!=0)
{
cout<<"ERROR!!!"<<endl;
}
int ret2=pthread_create(&pt2,0,func2,0);
if(ret2!=0)
{
cout<<"ERROR!!!"<<endl;
}
pthread_join(pt1,0);
pthread_join(pt2,0);
return 0;
}