c++ 单例模式多线程竞态条件

本文讨论了C++中单例模式在多线程环境下可能出现的竞态条件问题,导致内存泄露和可能的进程coredump。分析了由于 Singleton 实例化时内存分配失败引发的异常退出,并提出了避免竞态条件的策略,包括在程序启动时仅由主控线程实例化单例,或者使用互斥锁保护实例化过程。此外,还通过GDB分析了coredump的原因。
摘要由CSDN通过智能技术生成

单例模式在类只允许一个实例时用到

 

以下是摘自“Design Patterns Elements of Reusable Object-Oriented Software”一书中单例模式实现:

class Singleton {
public:
	static Singleton* Instance();
protected:
	Singleton(){};
private:
	static Singleton* _instance;
};

Singleton* Singleton::_instance = 0;

Singleton* Singleton::Instance () {
	if (_instance == 0) {
		_instance = new Singleton();
	}
	return _instance;
}

在Singleton::Instance ()中,_instance的检测与修改不是原子操作,存在竞态条件
当多个线程同时检测_instance,且此时_instance=0,这些线程都会进入if控制块,new会到堆中去为Singleton分配内存;由于这些线程都为Singleton分配了内存,且只有一个会赋值到_instance,其它的就无法引用而出现内存泄露。

 

用以下例子来扩大化问题,因内存泄露而造成进程的coredump

/* singletonrace.cpp */
  1 #include <iostream>
  2 #include <cstdlib>
  3 #include <sstream>
  4 #include <cerrno>
  5 #include <cstring>
  6 
  7 #define MAX 1024
  8 #define ARRAYSIZE 1024*1024*50
  9 
 10 class Singleton {
 11 public:
 12         static Singleton* Instance(int nsecs);
 13 protected:
 14         Singleton();
 15 private:
 16         static Singleton* _instance;
 17         int array[ARRAYSIZE];
 18 };
 19 
 20 Singleton* Singleton::_instance = 0;
 21 
 22 Singleton::Singleton() { }
 23 /*
 24 Singleton::Singleton() {
 25         int idx;
 26         for (idx = 0; idx < ARRAYSIZE; idx++) {
 27                 array[idx] = 0;
 28         }
 29 }
 30 */
 31 
 32 Singleton* Singleton::Instance (int threadId) {
 33         if (_instance == 0) {
 34                 ::sleep(threadId%32 + 3);
 35                  _instance = new Singleton();
 36         }
 37         return _instance;
 38 }
 39 
 40 int runningThread = 0;
 41 
 42 void * fun(void *id){
 43         runningThread++;
 44         int threadId = *(int *)id;
 45         Singleton::Instance(threadId);
 46         runningThread--;
 47 }
 48 
 49 int main(int argc, char *argv[])
 50 {
 51         int idx = 0;
 52         pthread_attr_t thrattr;
 53         pthre
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值