support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}
};

View 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;
}