简单的模拟多线程引用计数原理
2011-12-18 16:08
896 查看
大家都知道多线程编程学习中有一个很重要的东西——引用计数,一个线程的生或死或运行状态都跟这个计数有关,他同样是在适当的时候加加减减的。这篇文章的目的就是模拟下简单的引用计数,原因是因为项目中GateServer莫名宕机,而且运维没有给过来宕机详细信息中的偏移地址,所以纵然我们又cod文件也没法查找问题所在,所以就想出了这样一个笨办法,在每个函数都加上调用计数,这样超过一定次数的我们就认为它可能是死递归,从而方便确定问题。下面给出一个简单的引用计数类的代码。(没有写成模板,因为模板的理解成本有点高,所以在项目中不太使用)
C++
/************************************************************************
FileName:AutoRefCount.h
Author :eliteYang
URL :http://www.cppfans.org
Desc :引用计数
************************************************************************/
#ifndef __AUTO_REF_COUNT_H__
#define __AUTO_REF_COUNT_H__
#pragma once
class RefCount
{
public:
RefCount() : _refCount( 0 ){}
~RefCount(){}
void AddRefCount(){ ++_refCount; }
void DecRefCount()
{
--_refCount;
if ( _refCount < 0 )
{ _refCount = 0; }
}
int GetRefCount(){ return _refCount; }
private:
int _refCount;
};
class PtrRefCount
{
public:
PtrRefCount( RefCount* pRef, int nValue = 100 ) : pRefCount( pRef ), nCount( nValue )
{
if ( NULL != pRefCount )
{
pRefCount->AddRefCount();
}
}
~PtrRefCount()
{
if ( NULL != pRefCount )
{
pRefCount->DecRefCount();
}
}
bool CheckCount( char* szFunction )
{
if ( NULL == pRefCount )
{ return false; }
if ( pRefCount->GetRefCount() > nCount )
{
std::cout << "Function " << szFunction << " call error, maybe dead recursion, please check the code" << std::endl;
return false;
}
return true;
}
private:
RefCount* pRefCount;
const int nCount;
};
#endif
下载我们写一个例子来测试下,我们故意写一个死递归来检验代码。如下:
C++
#include "AutoRefCount.h"
#include <iostream>
#define __FUNCTION_CALL_COUNT__ RefCount _ref; \
PtrRefCount ptrRef( &_ref ); \
ptrRef.CheckCount( __FUNCTION__ );
void function()
{
__FUNCTION_CALL_COUNT__;
function();
}
int _tmain(int argc, _TCHAR* argv[])
{
function();
return 0;
}
结果我们发现打出了该函数可能死递归的Log,这样我们就方便查找问题了。希望对你有用!
C++
/************************************************************************
FileName:AutoRefCount.h
Author :eliteYang
URL :http://www.cppfans.org
Desc :引用计数
************************************************************************/
#ifndef __AUTO_REF_COUNT_H__
#define __AUTO_REF_COUNT_H__
#pragma once
class RefCount
{
public:
RefCount() : _refCount( 0 ){}
~RefCount(){}
void AddRefCount(){ ++_refCount; }
void DecRefCount()
{
--_refCount;
if ( _refCount < 0 )
{ _refCount = 0; }
}
int GetRefCount(){ return _refCount; }
private:
int _refCount;
};
class PtrRefCount
{
public:
PtrRefCount( RefCount* pRef, int nValue = 100 ) : pRefCount( pRef ), nCount( nValue )
{
if ( NULL != pRefCount )
{
pRefCount->AddRefCount();
}
}
~PtrRefCount()
{
if ( NULL != pRefCount )
{
pRefCount->DecRefCount();
}
}
bool CheckCount( char* szFunction )
{
if ( NULL == pRefCount )
{ return false; }
if ( pRefCount->GetRefCount() > nCount )
{
std::cout << "Function " << szFunction << " call error, maybe dead recursion, please check the code" << std::endl;
return false;
}
return true;
}
private:
RefCount* pRefCount;
const int nCount;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /************************************************************************ FileName:AutoRefCount.h Author :eliteYang URL :http://www.cppfans.org Desc :引用计数 ************************************************************************/ #ifndef __AUTO_REF_COUNT_H__ #define __AUTO_REF_COUNT_H__ #pragma once class RefCount { public: RefCount() : _refCount( 0 ){} ~RefCount(){} void AddRefCount(){ ++_refCount; } void DecRefCount() { --_refCount; if ( _refCount < 0 ) { _refCount = 0; } } int GetRefCount(){ return _refCount; } private: int _refCount; }; class PtrRefCount { public: PtrRefCount( RefCount* pRef, int nValue = 100 ) : pRefCount( pRef ), nCount( nValue ) { if ( NULL != pRefCount ) { pRefCount->AddRefCount(); } } ~PtrRefCount() { if ( NULL != pRefCount ) { pRefCount->DecRefCount(); } } bool CheckCount( char* szFunction ) { if ( NULL == pRefCount ) { return false; } if ( pRefCount->GetRefCount() > nCount ) { std::cout << "Function " << szFunction << " call error, maybe dead recursion, please check the code" << std::endl; return false; } return true; } private: RefCount* pRefCount; const int nCount; }; #endif |
C++
#include "AutoRefCount.h"
#include <iostream>
#define __FUNCTION_CALL_COUNT__ RefCount _ref; \
PtrRefCount ptrRef( &_ref ); \
ptrRef.CheckCount( __FUNCTION__ );
void function()
{
__FUNCTION_CALL_COUNT__;
function();
}
int _tmain(int argc, _TCHAR* argv[])
{
function();
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include "AutoRefCount.h" #include <iostream> #define __FUNCTION_CALL_COUNT__ RefCount _ref; \ PtrRefCount ptrRef( &_ref ); \ ptrRef.CheckCount( __FUNCTION__ ); void function() { __FUNCTION_CALL_COUNT__; function(); } int _tmain(int argc, _TCHAR* argv[]) { function(); return 0; } |
相关文章推荐
- 简单的模拟多线程引用计数原理
- 简单的模拟多线程引用计数原理
- C++虚析构函数和简单的引用计数原理
- HTTP网络请求原理 (三) 简单模拟HTTP服务器
- Java: 简单模拟多线程访问同样变量导致的问题
- 引用计数的原理和实例
- java 多线程,socket 简单模拟聊天软件
- java动态代理原理及简单模拟
- spring依赖注入原理的简单模拟
- 【ZOJ3950 The 17th Zhejiang University Programming Contest C】【简单日期模拟】How Many Nines 区间9个数计数
- Objective-C 引用计数原理
- 狗尾续貂:利用引用计数在多线程中安全释放资源
- spring依赖注入原理的简单模拟
- C++引用计数技术及智能指针的简单实现
- C++ 引用计数技术及智能指针的简单实现
- oc引用计数原理-引用计数相关变化
- PHP简单模拟多线程
- C++如何使用简单的引用计数
- JAVA学习第六十课 — UDP协议 &基于多线程模拟简单的QQ聊天程序
- 简单模拟Java中创建多线程的两种方法