简单的模拟多线程引用计数原理
2015-07-07 21:11
302 查看
大家都知道多线程编程学习中有一个很重要的东西——引用计数,一个线程的生或死或运行状态都跟这个计数有关,他同样是在适当的时候加加减减的。这篇文章的目的就是模拟下简单的引用计数,原因是因为项目中GateServer莫名宕机,而且运维没有给过来宕机详细信息中的偏移地址,所以纵然我们又cod文件也没法查找问题所在,所以就想出了这样一个笨办法,在每个函数都加上调用计数,这样超过一定次数的我们就认为它可能是死递归,从而方便确定问题。下面给出一个简单的引用计数类的代码。(没有写成模板,因为模板的理解成本有点高,所以在项目中不太使用)
[cpp]
/************************************************************************
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
[/cpp]
下载我们写一个例子来测试下,我们故意写一个死递归来检验代码。如下:
[cpp]
#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;
}
[/cpp]
结果我们发现打出了该函数可能死递归的Log,这样我们就方便查找问题了。希望对你有用!
[cpp]
/************************************************************************
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
[/cpp]
下载我们写一个例子来测试下,我们故意写一个死递归来检验代码。如下:
[cpp]
#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;
}
[/cpp]
结果我们发现打出了该函数可能死递归的Log,这样我们就方便查找问题了。希望对你有用!
相关文章推荐
- 输入边长n 输出n边长实心菱形 和n边长空心菱形
- 获取浏览器可视区域、屏幕的宽和高
- thinkphp中文验证码不能显示的问题
- [LeetCode] Remove Nth Node From End of List
- 大阪中文离线地图App上线
- 一个Java解析JSON字符串的例子
- HDU 1074 Doing Homework (状压DP + 路径记录)
- OpenGL学习脚印: 投影矩阵的推导
- 一.Spring学习之旅----初识Spring
- 【Arduino】8地点LED数码管(3461BS)
- 2015湖南省队集训DAY4——hoodle
- 带下拉刷新的瀑布流 不会oom
- SecureCRT 连接sqlplus删除键失效解决方法
- MySQL优化之——优化
- 《Opencv 入门教程》
- 关于SQL Server数据表的五种约束
- RCP: JDT 根据org.eclipse.jdt.core.IJavaElement对象获取org.eclipse.jdt.core.dom.ASTNode对象
- 内存只有4G的MBP要怎么破
- [LintCode] 删除链表中倒数第n个节点
- 组件化开发