posts - 4,  comments - 5,  trackbacks - 0

TCriticalSection的代码

#pragma once


//#include 
#include <afxmt.h>
#include 
<iostream>


namespace cj
{

class TCriticalSection
{
private:
    CRITICAL_SECTION FCriticalSection;
public:
    TCriticalSection(
void)
    
{
        InitializeCriticalSection(
&FCriticalSection);
    }

    
~TCriticalSection(void)
    
{
        DeleteCriticalSection(
&FCriticalSection);
    }

    
void Enter(void)
    
{
        EnterCriticalSection(
&FCriticalSection);
    }

    
void Leave(void)
    
{
        LeaveCriticalSection(
&FCriticalSection);
    }

}
;

}
;  //namespace cj

函数锁的主要类(TLocalCriticalSectionLock)有代码
#pragma once


#include 
"CriticalSection.h"


#define DeclareLocalCriticalSectionLock( A_CriticalSectionName )    \
    TCriticalSection A_CriticalSectionName;                    \
    friend 
class TLocalCriticalSectionLock;

#define TriggerLocalCriticalSectionLock( A_CriticalSectionName )        \
    TLocalCriticalSectionLock FunctionCriticalSectionLock(
this->A_CriticalSectionName);


namespace cj
{

class TLocalCriticalSectionLock
{
private:
    TCriticalSection 
*FCriticalSection;
public:
    inline TLocalCriticalSectionLock(TCriticalSection 
&ACriticalSection):FCriticalSection(&ACriticalSection)
    
{
        FCriticalSection
->Enter();
    }

    inline 
~TLocalCriticalSectionLock(void)
    
{
        FCriticalSection
->Leave();
    }


}
;

}
// namespace cj

测试程序
// CritialLock.cpp : 定义控制台应用程序的入口点。
//


//#include "stdafx.h"
//#include "CriticalSection.h"

#include 
"LocalCriticalSectionLock.h"
#include 
<winbase.h>
#include 
<process.h>
#include 
<iostream>

using namespace cj;

//-------------------------------------------------------------------------------------------

const int ThreadHandleListSize = 10;

//-------------------------------------------------------------------------------------------
class GObject
{
private:
    DeclareLocalCriticalSectionLock(FFunctionLock)
public:
    
int GetInteger(void)
    
{
        TriggerLocalCriticalSectionLock(FFunctionLock);
        
//TLocalCriticalSectionLock lcsl(this->cs);
        std::cout<<"Enter************************"<<std::endl;
        std::cout
<<"GetInteger"<<std::endl;
        std::cout
<<"Leave||||||||||||||||||||||||"<<std::endl;
        
return 1;
    }

}
;

//-------------------------------------------------------------------------------------------
    GObject obj;

//-------------------------------------------------------------------------------------------
void _cdecl ThreadFunction(void*)
{
    
for (int i=0;i<20;i++)
    
{
        obj.GetInteger();
    }

    _endthread();
}


//-------------------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
    uintptr_t ThreadHandleList[ThreadHandleListSize];
    
    
for(int i=0;i<ThreadHandleListSize;i++)
    
{
        ThreadHandleList[i] 
= -1;
    }


    
for(int i=0;i<ThreadHandleListSize;i++)
    
{
        ThreadHandleList[i] 
= _beginthread(ThreadFunction,1024,0);
    }

    
    std::cin.
get();
    
return 0;
}



posted on 2005-07-11 01:33 Sunlea 的空间 阅读(640) 评论(0)  编辑  收藏 所属分类: C/C++

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

C#

  • Sunmast
  • MVP, C# 所属领域:Visual Developer - .NET, Visual Developer - Visual C#

C/C++

Delphi

forum

Other

Program

Software Technology

  • MVPcn.Net Blogs
  • MVPcn.Net Blogs
  • TDD
  • 大象的博客
  • 于淼 CSDN帐号:CMIC 所属领域:Visual Developer-XML Web Services 个人blog:http://www.donews.net/cmic

搜索

  •  

最新评论

阅读排行榜

评论排行榜