update
This commit is contained in:
46
design_pattern/singleton/barrier_singleton.cpp
Normal file
46
design_pattern/singleton/barrier_singleton.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by light on 20-2-7.
|
||||
//
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#define barrier() __asm__ volatile ("lwsync")
|
||||
|
||||
// method 1 operator new + placement new
|
||||
//singleton *instance() {
|
||||
// if (p == nullptr) {
|
||||
// lock_guard<mutex> guard(lock_);
|
||||
// if (p == nullptr) {
|
||||
// singleton *tmp = static_cast<singleton *>(operator new(sizeof(singleton)));
|
||||
// new(p)singleton();
|
||||
// p = tmp;
|
||||
// }
|
||||
// }
|
||||
// return p;
|
||||
//}
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
public:
|
||||
static singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
|
||||
singleton *singleton::instance() {
|
||||
if (p == nullptr) {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
barrier();
|
||||
if (p == nullptr) {
|
||||
p = new singleton();
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
46
design_pattern/singleton/cpulpuls11_singleton.cpp
Normal file
46
design_pattern/singleton/cpulpuls11_singleton.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by light on 20-2-7.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
|
||||
//C++ 11版本之后的跨平台实现
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
|
||||
static mutex lock_;
|
||||
static atomic<singleton *> p;
|
||||
public:
|
||||
singleton *instance();
|
||||
};
|
||||
|
||||
mutex singleton::lock_;
|
||||
atomic<singleton *> singleton::p;
|
||||
|
||||
/*
|
||||
* std::atomic_thread_fence(std::memory_order_acquire);
|
||||
* std::atomic_thread_fence(std::memory_order_release);
|
||||
* 这两句话可以保证他们之间的语句不会发生乱序执行。
|
||||
*/
|
||||
singleton *singleton::instance() {
|
||||
singleton *tmp = p.load(memory_order_relaxed);
|
||||
atomic_thread_fence(memory_order_acquire);
|
||||
if (tmp == nullptr) {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
tmp = p.load(memory_order_relaxed);
|
||||
if (tmp == nullptr) {
|
||||
tmp = new singleton();
|
||||
atomic_thread_fence(memory_order_release);
|
||||
p.store(tmp, memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
42
design_pattern/singleton/dcl_singleton.cpp
Normal file
42
design_pattern/singleton/dcl_singleton.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by light on 20-2-7.
|
||||
//
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <mutex>
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
public:
|
||||
singleton *instance();
|
||||
|
||||
// 实现一个内嵌垃圾回收类
|
||||
class CGarbo
|
||||
{
|
||||
public:
|
||||
~CGarbo()
|
||||
{
|
||||
if(singleton::p)
|
||||
delete singleton::p;
|
||||
}
|
||||
};
|
||||
static CGarbo Garbo; // 定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数从而释放单例对象
|
||||
};
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
singleton::CGarbo Garbo;
|
||||
|
||||
singleton* singleton::instance() {
|
||||
if (p == nullptr) {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
}
|
||||
return p;
|
||||
}
|
17
design_pattern/singleton/hungrysingleton.cpp
Normal file
17
design_pattern/singleton/hungrysingleton.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by light on 20-2-6.
|
||||
//
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
public:
|
||||
static singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::p = new singleton();
|
||||
singleton* singleton::instance() {
|
||||
return p;
|
||||
}
|
||||
|
19
design_pattern/singleton/iazysingleton.cpp
Normal file
19
design_pattern/singleton/iazysingleton.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by light on 20-2-6.
|
||||
//
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
public:
|
||||
static singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
|
||||
singleton* singleton::instance() {
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
return p;
|
||||
}
|
25
design_pattern/singleton/lock_singleton.cpp
Normal file
25
design_pattern/singleton/lock_singleton.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by light on 20-2-7.
|
||||
//
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include <mutex>
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
public:
|
||||
static singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
|
||||
singleton* singleton::instance() {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
return p;
|
||||
}
|
28
design_pattern/singleton/pthreadoncesingleton.cpp
Normal file
28
design_pattern/singleton/pthreadoncesingleton.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by light on 20-2-6.
|
||||
//
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <pthread.h>
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton(); //私有构造函数,不允许使用者自己生成对象
|
||||
singleton(const singleton &other);
|
||||
|
||||
//要写成静态方法的原因:类成员函数隐含传递this指针(第一个参数)
|
||||
static void init() {
|
||||
p = new singleton();
|
||||
}
|
||||
|
||||
static pthread_once_t ponce_;
|
||||
static singleton *p; //静态成员变量
|
||||
public:
|
||||
singleton *instance() {
|
||||
// init函数只会执行一次
|
||||
pthread_once(&ponce_, &singleton::init);
|
||||
return p;
|
||||
}
|
||||
};
|
||||
|
||||
|
22
design_pattern/singleton/static_local_singleton.cpp
Normal file
22
design_pattern/singleton/static_local_singleton.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by light on 20-2-7.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
static singleton *p;
|
||||
singleton() {}
|
||||
public:
|
||||
singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::instance() {
|
||||
static singleton p;
|
||||
return &p;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user