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 的空間 閱讀(645) 評論(0)  編輯  收藏 所屬分類: C/C++

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(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

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宜宾市| 错那县| 谢通门县| 夏津县| 富锦市| 哈巴河县| 沁阳市| 台中市| 泰宁县| 灵璧县| 都匀市| 天等县| 古丈县| 错那县| 周至县| 江津市| 舞钢市| 阜南县| 石林| 宜君县| 义马市| 裕民县| 福泉市| 木兰县| 盐池县| 临朐县| 原平市| 鹤庆县| 柳林县| 青州市| 西畴县| 武邑县| 新野县| 灵台县| 喀喇沁旗| 长春市| 云和县| 惠安县| 庐江县| 建德市| 图们市|