您的位置:首页 > 编程语言 > C语言/C++

简单的模拟多线程引用计数原理

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
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;

}
结果我们发现打出了该函数可能死递归的Log,这样我们就方便查找问题了。希望对你有用!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息