#include <iostream>
#include <unistd.h>
using namespace std;
class CSingle_lh{
public:
static CSingle_lh* getInstance()
{
if (NULL == m_pins)
{
m_pins = new CSingle_lh;
m_cnt++;
}
return m_pins;
}
static void printCnt()
{
cout<<"m_cnt:"<<m_cnt<<"m_pins:"<<m_pins<<endl;
}
static CSingle_lh* releaseInstance()
{
if (m_pins != NULL)
{
delete m_pins;
m_pins = NULL;
}
return m_pins;
}
private:
CSingle_lh()
{
m_cnt = 0;
m_pins = NULL;
sleep(5);
}
CSingle_lh(const CSingle_lh& cs){;}
CSingle_lh& operator=(const CSingle_lh& cs){;}
private:
static CSingle_lh* m_pins;
static int m_cnt;
};
int CSingle_lh::m_cnt = 0;
CSingle_lh* CSingle_lh::m_pins = NULL;
void test1()
{
CSingle_lh* pins = CSingle_lh::getInstance();
pins->printCnt();
pins->releaseInstance();
}
void* func(void* arg)
{
CSingle_lh* pins = CSingle_lh::getInstance();
pins->printCnt();
pins->releaseInstance();
}
void test2()
{
int i = 0;
pthread_t id[10];
for(i = 0; i<10; i++)
{
pthread_create(&id[i], NULL, func, NULL);
}
for(i = 0; i<10; i++)
{
pthread_join(id[i], NULL);
}
}
int main()
{
test2();
return 0;
}
test2()多线程运行结果,发现对象确实多次创建,线程不安全:
m_cnt:1m_pins:0x7f8e9c0008c0
m_cnt:2m_pins:0x7f8e940008c0
m_cnt:3m_pins:0x7f8e980008c0
m_cnt:4m_pins:0x7f8e8c0008c0
m_cnt:5m_pins:0x7f8e900008c0
m_cnt:6m_pins:0x7f8e840008c0
m_cnt:7m_pins:0x7f8e880008c0
m_cnt:8m_pins:0x7f8e7c0008c0
m_cnt:9m_pins:0x2052730
m_cnt:10m_pins:0x7f8e7c0008e0
#include <iostream>
#include <unistd.h>
using namespace std;
class CSingle_eh{
public:
static CSingle_eh* getInstance()
{
return m_pins;
}
static void printCnt()
{
cout<<"m_cnt:"<<m_cnt<<"m_pins:"<<m_pins<<endl;
}
static CSingle_eh* releaseInstance()
{
if (m_pins != NULL)
{
delete m_pins;
m_pins = NULL;
}
return NULL;
}
private:
CSingle_eh()
{
m_cnt++;
m_pins = NULL;
sleep(2);
}
CSingle_eh(const CSingle_eh& cs){;}
CSingle_eh& operator=(const CSingle_eh& cs){;}
private:
static CSingle_eh* m_pins;
static int m_cnt;
};
int CSingle_eh::m_cnt = 0;
CSingle_eh* CSingle_eh::m_pins = new CSingle_eh;
void test1()
{
CSingle_eh* pins = CSingle_eh::getInstance();
pins->printCnt();
pins->releaseInstance();
}
void* func(void* arg)
{
CSingle_eh* pins = CSingle_eh::getInstance();
pins->printCnt();
if (pins != NULL)
{
pins->releaseInstance();
}
}
void test2()
{
int i = 0;
pthread_t id[10];
for(i = 0; i<10; i++)
{
pthread_create(&id[i], NULL, func, NULL);
}
for(i = 0; i<10; i++)
{
pthread_join(id[i], NULL);
}
}
int main()
{
test2();
return 0;
}
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
class CLock{
public:
CLock()
{
pthread_mutex_init(&m_mutex, NULL);
}
~CLock()
{
pthread_mutex_destroy(&m_mutex);
}
void lock()
{
pthread_mutex_lock(&m_mutex);
}
void unlock()
{
pthread_mutex_unlock(&m_mutex);
}
private:
pthread_mutex_t m_mutex;
};
CLock lk;
class CSingle_lh{
public:
static CSingle_lh* getInstance()
{
if (NULL == m_pins)
{
pthread_mutex_lock(&m_mutex);
if (NULL == m_pins)
{
m_pins = new CSingle_lh;
m_cnt++;
}
pthread_mutex_unlock(&m_mutex);
}
return m_pins;
}
static void printCnt()
{
cout<<"m_cnt:"<<m_cnt<<"m_pins:"<<m_pins<<endl;
}
static CSingle_lh* releaseInstance()
{
if (m_pins != NULL)
{
delete m_pins;
m_pins = NULL;
}
return m_pins;
}
private:
CSingle_lh()
{
m_cnt = 0;
m_pins = NULL;
sleep(2);
}
CSingle_lh(const CSingle_lh& cs){;}
CSingle_lh& operator=(const CSingle_lh& cs){;}
private:
static CSingle_lh* m_pins;
static int m_cnt;
static pthread_mutex_t m_mutex;
};
int CSingle_lh::m_cnt = 0;
CSingle_lh* CSingle_lh::m_pins = NULL;
pthread_mutex_t CSingle_lh::m_mutex = PTHREAD_MUTEX_INITIALIZER;
void test1()
{
CSingle_lh* pins = CSingle_lh::getInstance();
pins->printCnt();
pins->releaseInstance();
}
void* func(void* arg)
{
CSingle_lh* pins = CSingle_lh::getInstance();
pins->printCnt();
pins->releaseInstance();
}
void test2()
{
int i = 0;
pthread_t id[10];
for(i = 0; i<10; i++)
{
pthread_create(&id[i], NULL, func, NULL);
}
for(i = 0; i<10; i++)
{
pthread_join(id[i], NULL);
}
}
int main()
{
test2();
return 0;
}