C++多线程单例模式

01 原理

单例模式即是私有化构造函数,创建时通过类中静态函数获得单一实例的一种特殊类。在多线程下需要加上互斥锁,保证单例模式的创建和析构是原子性的。

由于单例模式的特殊性,这里不讨论其析构函数,个人在具体使用的环境中不需要析构单例模式的实例。

02 结果

创建十个线程来测试。

singleton born
PID:: 0x70000c545000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c8da000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c64b000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c6ce000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c751000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c7d4000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c857000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c95d000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c9e0000 ------ instance address is:: 0x7fb58f40fb30
PID:: 0x70000c5c8000 ------ instance address is:: 0x7fb58f40fb30

03 Show me the code

#include <iostream>
#include <thread>
#include <mutex>


using namespace std;


// ===========================================
// 用于单例模式的互斥锁
mutex mt;
// 是否存在实例的标识位
bool isExist = false;

// 单例模式
class Singleton {
public:
    // 存放实例地址的指针
    static Singleton* one_singleton;

    // 需要调用实例的地方调用该函数
    static Singleton* get_singleton() {
        if (isExist) {
            return one_singleton;
        } else {
            lock_guard<mutex> lg(mt);
            one_singleton = new Singleton();
            cout << "singleton born" << endl;
            isExist = true;
            return one_singleton;
        }
    }
    
    // 析构函数
    ~Singleton() {}

private:
    // 构造函数私有
    explicit Singleton() {}
};

// static变量类外初始化
Singleton* Singleton::one_singleton = nullptr;
// ==============================================


// 防止终端显示混乱的互斥锁
mutex log;
// 子线程调用函数,获得单例模式实例并输出其内存地址
void* show(void*) {
    lock_guard<mutex> lg(log);
    Singleton* one = Singleton::get_singleton();
    this_thread::sleep_for(chrono::microseconds(10));
    cout << "PID:: " << this_thread::get_id() << " ------ instance address is:: " << one << endl;
    
    return nullptr;
}


int main() {
    // 创建10个线程调用show函数
    for (int i = 0; i < 10; i++) {
        pthread_t pthread;
        pthread_create(&pthread, NULL, show, NULL);
    }

    // 主线程延时等待以上线程执行完毕
    this_thread::sleep_for(chrono::microseconds(1000));
    
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值