c++使用标准库函数call_once进行单例模式初始化

前言

        单例模式有懒汉式和饿汉式。

        两者的主要区别在于:

  •         懒汉式:第一次调用时,才会进行初始化;
  •         饿汉式:程序加载时,就会进行初始化;

        懒汉式和饿汉式还有一个不同点:

                 饿汉式是线程安全的,懒汉式不是。在多线程环境下,懒汉式的线程安全问题是一个重要问题。

内容

        常见的处理懒汉式的线程安全问题一般是 加锁 和  双重检查

#include "iostream"
#include "shared_mutex"
#include "mutex"
using namespace std;

class Singleton
{
public:
    static Singleton *Instance()
    {
        if (ptr == nullptr)
        {
            std::shared_lock<shared_mutex> lock(m_mutex);
            if(ptr == nullptr)
            {
                ptr = new Singleton;
            }
        }
        return ptr;
    }

private:
    static Singleton *ptr;
    static shared_mutex m_mutex;

private:
    Singleton() = default;
    Singleton(const Singleton &) = delete;
    Singleton &operator=(const Singleton &) = delete;
};
Singleton *Singleton::ptr = nullptr;

 我们也可以使用标准库中的call_once来进行单例懒汉式初始化

#include "iostream"
#include "shared_mutex"
#include "mutex"
#include "thread"
using namespace std;

class Singleton
{
public:
    static Singleton *Instance()
    {
        if(ptr == nullptr)
        {
            std::call_once(flag, [&]()
            {
                ptr = new Singleton;
            });
        }
        else
        {
            cout<<"hello mama"<<endl;
        }
        return ptr;
    }

private:
    static Singleton *ptr;
    static std::once_flag flag;
private:
    Singleton(){
        cout<<"hello word"<<endl;
    }
    ~Singleton(){
        delete ptr;
        ptr = nullptr;
    }
    Singleton(const Singleton &) = delete;
    Singleton &operator=(const Singleton &) = delete;
};
Singleton *Singleton::ptr = nullptr;

std::once_flag Singleton::flag;

void test(void)
{
    Singleton::Instance();
}

int main(void)
{
    std::thread t1(test);
    std::thread t2(test);
    std::thread t3(test);
    t1.join();
    t2.join();
    t3.join();
    cout << "end" << endl;
    return 0;
}

单例模式中使用call_once(),让模块在第一次被线程调用时就进行初始化

使用std::call_once函数时,首先需要创建一个std::once_flag对象,用于标记函数是否已经被调用。然后,在多个线程中调用std::call_once函数,并传入相同的std::once_flag对象和要执行的函数。在第一个调用std::call_once的线程中,函数会被执行一次,而在其它调用线程中,函数会被跳过。 

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直在找资料的菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值