support bazel complie this project and format code.
This commit is contained in:
@@ -7,40 +7,39 @@ using namespace std;
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#define barrier() __asm__ volatile ("lwsync")
|
||||
#define barrier() __asm__ volatile("lwsync")
|
||||
|
||||
// method 1 operator new + placement new
|
||||
//singleton *instance() {
|
||||
// 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;
|
||||
// singleton *tmp = static_cast<singleton *>(operator
|
||||
// new(sizeof(singleton))); new(p)singleton(); p = tmp;
|
||||
// }
|
||||
// }
|
||||
// return p;
|
||||
//}
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
singleton() {}
|
||||
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
public:
|
||||
static singleton *instance();
|
||||
static singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
|
||||
singleton *singleton::instance() {
|
||||
if (p == nullptr) {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
barrier();
|
||||
if (p == nullptr) {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
barrier();
|
||||
if (p == nullptr) {
|
||||
p = new singleton();
|
||||
}
|
||||
p = new singleton();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@@ -6,41 +6,40 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
|
||||
//C++ 11版本之后的跨平台实现
|
||||
// C++ 11版本之后的跨平台实现
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
singleton() {}
|
||||
|
||||
static mutex lock_;
|
||||
static atomic<singleton *> p;
|
||||
|
||||
static mutex lock_;
|
||||
static atomic<singleton *> p;
|
||||
public:
|
||||
singleton *instance();
|
||||
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);
|
||||
* 这两句话可以保证他们之间的语句不会发生乱序执行。
|
||||
*/
|
||||
* 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);
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
tmp = new singleton();
|
||||
atomic_thread_fence(memory_order_release);
|
||||
p.store(tmp, memory_order_relaxed);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -9,35 +9,35 @@ using namespace std;
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
singleton() {}
|
||||
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
public:
|
||||
static singleton *instance();
|
||||
static singleton *instance();
|
||||
|
||||
// 实现一个内嵌垃圾回收类
|
||||
class CGarbo
|
||||
{
|
||||
public:
|
||||
~CGarbo()
|
||||
{
|
||||
if(singleton::p)
|
||||
delete singleton::p;
|
||||
}
|
||||
};
|
||||
static CGarbo Garbo; // 定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数从而释放单例对象
|
||||
// 实现一个内嵌垃圾回收类
|
||||
class CGarbo {
|
||||
public:
|
||||
~CGarbo() {
|
||||
if (singleton::p)
|
||||
delete singleton::p;
|
||||
}
|
||||
};
|
||||
static CGarbo
|
||||
Garbo; // 定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数从而释放单例对象
|
||||
};
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
singleton::CGarbo Garbo;
|
||||
std::mutex singleton::lock_;
|
||||
|
||||
singleton* singleton::instance() {
|
||||
if (p == nullptr) {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
}
|
||||
return p;
|
||||
singleton *singleton::instance() {
|
||||
if (p == nullptr) {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
@@ -4,14 +4,12 @@
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
|
||||
public:
|
||||
static singleton *instance();
|
||||
static singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::p = new singleton();
|
||||
singleton* singleton::instance() {
|
||||
return p;
|
||||
}
|
||||
|
||||
singleton *singleton::instance() { return p; }
|
||||
|
@@ -4,16 +4,17 @@
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
|
||||
public:
|
||||
static singleton *instance();
|
||||
static singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
|
||||
singleton* singleton::instance() {
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
return p;
|
||||
singleton *singleton::instance() {
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
return p;
|
||||
}
|
||||
|
@@ -8,18 +8,19 @@ using namespace std;
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
singleton() {}
|
||||
static singleton *p;
|
||||
static mutex lock_;
|
||||
|
||||
public:
|
||||
static singleton *instance();
|
||||
static singleton *instance();
|
||||
};
|
||||
|
||||
singleton *singleton::p = nullptr;
|
||||
|
||||
singleton* singleton::instance() {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
return p;
|
||||
singleton *singleton::instance() {
|
||||
lock_guard<mutex> guard(lock_);
|
||||
if (p == nullptr)
|
||||
p = new singleton();
|
||||
return p;
|
||||
}
|
||||
|
@@ -2,27 +2,23 @@
|
||||
// Created by light on 20-2-6.
|
||||
//
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton(); //私有构造函数,不允许使用者自己生成对象
|
||||
singleton(const singleton &other);
|
||||
singleton(); //私有构造函数,不允许使用者自己生成对象
|
||||
singleton(const singleton &other);
|
||||
|
||||
//要写成静态方法的原因:类成员函数隐含传递this指针(第一个参数)
|
||||
static void init() {
|
||||
p = new singleton();
|
||||
}
|
||||
//要写成静态方法的原因:类成员函数隐含传递this指针(第一个参数)
|
||||
static void init() { p = new singleton(); }
|
||||
|
||||
static pthread_once_t ponce_;
|
||||
static singleton *p; //静态成员变量
|
||||
static pthread_once_t ponce_;
|
||||
static singleton *p; //静态成员变量
|
||||
public:
|
||||
singleton *instance() {
|
||||
// init函数只会执行一次
|
||||
pthread_once(&ponce_, &singleton::init);
|
||||
return p;
|
||||
}
|
||||
singleton *instance() {
|
||||
// init函数只会执行一次
|
||||
pthread_once(&ponce_, &singleton::init);
|
||||
return p;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// Created by light on 20-2-7.
|
||||
// 在C++11标准下,《Effective C++》提出了一种更优雅的单例模式实现,使用函数内的 local static 对象。
|
||||
// 这种方法也被称为Meyers' Singleton。
|
||||
// 在C++11标准下,《Effective C++》提出了一种更优雅的单例模式实现,使用函数内的
|
||||
// local static 对象。 这种方法也被称为Meyers' Singleton。
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
@@ -10,14 +10,13 @@ using namespace std;
|
||||
|
||||
class singleton {
|
||||
private:
|
||||
singleton() {}
|
||||
singleton() {}
|
||||
|
||||
public:
|
||||
static singleton &instance();
|
||||
static singleton &instance();
|
||||
};
|
||||
|
||||
singleton &singleton::instance() {
|
||||
static singleton p;
|
||||
return p;
|
||||
static singleton p;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user