C语言 多线程单例模式,【C++并发与多线程】 7_单例设计模式、call_once

懒汉式单例类#include

#include

#include

using namespace std;

class Singleton

{

public:

static Singleton* getInstance();

Singleton(const Singleton&) = delete;

Singleton& operator=(const Singleton&) = delete;

private:

Singleton() = default;

static Singleton* instance;

static mutex mtx;

};

Singleton *Singleton::instance = nullptr;

mutex Singleton::mtx;

Singleton* Singleton::getInstance()

{

if (instance == nullptr)

{

lock_guard lck(mtx); // 注意这里 !!!

if (instance == nullptr)

{

instance = new Singleton();

}

}

return instance;

}

void thread_func()

{

Singleton* p = Singleton::getInstance();

// p-> do something ...

}

int main()

{

thread th1(thread_func);

thread th2(thread_func);

th1.join();

th2.join();

return 0;

}

饿汉式单例类#include

#include

using namespace std;

class Singleton

{

public:

static Singleton* getInstance();

Singleton(const Singleton&) = delete;

Singleton& operator=(const Singleton&) = delete;

private:

Singleton() = default;

static Singleton* instance;

};

Singleton *Singleton::instance = new Singleton(); // 注意这里 !!!

Singleton* Singleton::getInstance()

{

return instance;

}

void thread_func()

{

Singleton* p = Singleton::getInstance();

// p-> do something ...

}

int main()

{

thread th1(thread_func);

thread th2(thread_func);

th1.join();

th2.join();

return 0;

}

懒汉式的 call_once 实现template

void call_once (once_flag& flag, Fn&& fn, Args&&... args);准确执行一次可调用对象 fn ,即使同时从多个线程调用。#include

#include

#include

using namespace std;

class Singleton

{

public:

static Singleton* getInstance();

Singleton(const Singleton&) = delete;

Singleton& operator=(const Singleton&) = delete;

private:

Singleton() = default;

static void createInstance();

static Singleton* instance;

static once_flag onceFlag;

};

void Singleton::createInstance()

{

instance = new Singleton();

}

Singleton *Singleton::instance = nullptr;

once_flag Singleton::onceFlag;

Singleton* Singleton::getInstance()

{

call_once(onceFlag, createInstance); // 注意这里 !!!

return instance;

}

void thread_func()

{

Singleton* p = Singleton::getInstance();

// p-> do something ...

}

int main()

{

thread th1(thread_func);

thread th2(thread_func);

th1.join();

th2.join();

return 0;

}

单例类的资源清理【饿、懒同理】#include

#include

#include

using namespace std;

class Singleton

{

struct Recycle

{

~Recycle() // 注意这里 !!!

{

if (instance != nullptr)

{

delete instance;

instance = nullptr;

}

}

};

public:

static Singleton* getInstance();

Singleton(const Singleton&) = delete;

Singleton& operator=(const Singleton&) = delete;

private:

Singleton() = default;

~Singleton();

static Singleton* instance;

static mutex mtx;

};

Singleton *Singleton::instance = nullptr;

mutex Singleton::mtx;

Singleton* Singleton::getInstance()

{

if (instance == nullptr)

{

lock_guard lck(mtx);

if (instance == nullptr)

{

instance = new Singleton();

static Recycle recycle;

}

}

return instance;

}

Singleton::~Singleton() // 注意这里 !!!

{

// 单例类资源清理

}

void thread_func()

{

Singleton* p = Singleton::getInstance();

// p-> do something ...

}

int main()

{

thread th1(thread_func);

thread th2(thread_func);

th1.join();

th2.join();

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值