当前位置:网站首页>Thread safe sigleton (singleton mode)
Thread safe sigleton (singleton mode)
2022-04-23 20:59:00 【Descosmos】
The singleton pattern
The definition of singleton pattern and its most basic implementation will not be mentioned more , It's important to point out that , For general singleton mode , On the basis of if(hold_ == NULL) It is obviously difficult to meet the requirements of thread safety , Therefore, this article through pthread_once To achieve .
Be careful , You can also use thread synchronization mechanism , Like locks , Semaphore, etc .
template<typename T>
class Signleton: boost noncopyable{
public:
static T& instance(){
pthead_once(&ponce_, &Signleton::init);
return *value_;
}
private:
// Set constructors and destructors to private, So that singleton mode cannot be created on the stack
Signleton();
~Signleton();
static void init(){
value_ = new T();
}
private:
static pthread_once_t ponce_;
static T* value_; // Data in singleton mode
};
// Must be defined in the header file static Variable
pthread_once_t Signleton<T>::ponce_ = PTHREAD_ONCE_INIT;
template<typename T>
T* Signleton<T>::value_ NULL;
Usage method
Foo &foo = Sigleton<Foo>::instance();
Be careful
This singleton pattern does not consider how to destroy objects .
版权声明
本文为[Descosmos]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210545298121.html
边栏推荐
- MySQL stored procedures and functions
- Win 11K in 100 days, super complete learning guide for job transfer test
- Learn to C language fourth day
- 如何发挥测试策略的指导性作用
- MySQL进阶之数据的增删改查(DML)
- Thinking after learning to type
- 常用60类图表使用场景、制作工具推荐
- ROS学习笔记-----ROS的使用教程
- Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
- Is rust more suitable for less experienced programmers?
猜你喜欢
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
Prim、Kruskal
Summary and effect analysis of methods for calculating binocular parallax
MySQL进阶之表的增删改查
Recommended usage scenarios and production tools for common 60 types of charts
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
又一款数据分析神器:Polars 真的很强大
Problem brushing plan -- dynamic programming (IV)
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
Minecraft 1.12.2 module development (43) custom shield
随机推荐
laravel 发送邮件
mmap、munmap
Send email to laravel
Google tries to use rust in Chrome
How to learn software testing? Self study or training? After reading this article, you will understand
Thinkphp5 + data large screen display effect
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
Tensorflow realizes gradient accumulation, and then returns
flomo软件推荐
Awk example skills
How to play the guiding role of testing strategy
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
Alibaba cloud responded to the disclosure of user registration information
Ubuntu 20 installing centernet
Unity asset import settings
电脑越用越慢怎么办?文件误删除恢复方法
PHP的Laravel与Composer部署项目时常见问题
Mysql database common sense storage engine
深入探究ASP.NET Core读取Request.Body的正确方式
管道和xargs