forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b5e3bc
commit 60af660
Showing
17 changed files
with
505 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
Oops, something went wrong.