support bazel complie this project and format code.
This commit is contained in:
8
design_pattern/producer_consumer/BUILD
Normal file
8
design_pattern/producer_consumer/BUILD
Normal file
@@ -0,0 +1,8 @@
|
||||
# please run `bazel run //design_pattern/producer_consumer:producer_consumer`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "producer_consumer",
|
||||
srcs = ["producer_consumer.cpp"],
|
||||
)
|
@@ -1,16 +1,15 @@
|
||||
#include <condition_variable>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MAX_NUM = 10000;
|
||||
|
||||
class BoundedBuffer
|
||||
{
|
||||
class BoundedBuffer {
|
||||
public:
|
||||
BoundedBuffer(size_t n) {
|
||||
array_.resize(n);
|
||||
@@ -21,7 +20,7 @@ public:
|
||||
void Produce(int n) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mtx_);
|
||||
//wait for not full
|
||||
// wait for not full
|
||||
not_full_.wait(lock, [=] { return pos_ < array_.size(); });
|
||||
|
||||
usleep(1000 * 400);
|
||||
@@ -29,16 +28,16 @@ public:
|
||||
end_pos_ = (end_pos_ + 1) % array_.size();
|
||||
++pos_;
|
||||
cout << "Produce pos:" << pos_ << endl;
|
||||
} //auto unlock
|
||||
} // auto unlock
|
||||
|
||||
not_empty_.notify_one();
|
||||
}
|
||||
|
||||
|
||||
int Consume() {
|
||||
std::unique_lock<std::mutex> lock(mtx_);
|
||||
//wait for not empty
|
||||
// wait for not empty
|
||||
not_empty_.wait(lock, [=] { return pos_ > 0; });
|
||||
|
||||
|
||||
usleep(1000 * 400);
|
||||
int n = array_[start_pos_];
|
||||
start_pos_ = (start_pos_ + 1) % array_.size();
|
||||
@@ -50,6 +49,7 @@ public:
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<int> array_;
|
||||
size_t start_pos_;
|
||||
@@ -63,10 +63,9 @@ private:
|
||||
BoundedBuffer bb(10);
|
||||
std::mutex g_mtx;
|
||||
|
||||
void Producer()
|
||||
{
|
||||
void Producer() {
|
||||
int n = 0;
|
||||
while(n < 100) {
|
||||
while (n < 100) {
|
||||
bb.Produce(n);
|
||||
cout << "Producer:" << n << endl;
|
||||
n++;
|
||||
@@ -75,28 +74,25 @@ void Producer()
|
||||
bb.Produce(-1);
|
||||
}
|
||||
|
||||
|
||||
void Consumer()
|
||||
{
|
||||
void Consumer() {
|
||||
std::thread::id thread_id = std::this_thread::get_id();
|
||||
int n = 0;
|
||||
do {
|
||||
n = bb.Consume();
|
||||
cout << "Consumer thread:" << thread_id << "=====> " << n << endl;
|
||||
} while(-1 != n);
|
||||
} while (-1 != n);
|
||||
|
||||
bb.Produce(-1);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
std::vector<std::thread> t;
|
||||
t.push_back(std::thread(&Producer));
|
||||
t.push_back(std::thread(&Consumer));
|
||||
t.push_back(std::thread(&Consumer));
|
||||
t.push_back(std::thread(&Consumer));
|
||||
|
||||
for (auto& one : t) {
|
||||
|
||||
for (auto &one : t) {
|
||||
one.join();
|
||||
}
|
||||
|
||||
|
@@ -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