#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t lock;
class Singleton{
private:
Singleton(){}
static Singleton* instance;
public:
~Singleton(){}
static Singleton* getInstance(){
if(instance==NULL){
pthread_mutex_lock(&lock);
if(instance==NULL){
instance=new Singleton;
printf("obj created.\n");
}
pthread_mutex_unlock(&lock);
}
return instance;
}
};
Singleton* Singleton::instance = NULL;
//Singleton* Singleton::instance = new Singleton;
void* fun1(void*){
Singleton::getInstance();
printf("%d\n", 1);
}
void* fun2(void*){
Singleton::getInstance();
printf("%d\n", 2);
}
int main(void){
pthread_mutex_init(&lock, NULL);
pthread_t pid1, pid2;
pthread_create(&pid1, NULL, fun1, NULL);
pthread_create(&pid2, NULL, fun2, NULL);
pthread_join(pid1, NULL);
pthread_join(pid2, NULL);
pthread_mutex_destroy(&lock);
return 0;
}
C++单例模式
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。