CriticalSection.h
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:3k
源码类别:

P2P编程

开发平台:

Visual C++

  1. /*
  2.  *  Openmysee
  3.  *
  4.  *  This program is free software; you can redistribute it and/or modify
  5.  *  it under the terms of the GNU General Public License as published by
  6.  *  the Free Software Foundation; either version 2 of the License, or
  7.  *  (at your option) any later version.
  8.  *
  9.  *  This program is distributed in the hope that it will be useful,
  10.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *  GNU General Public License for more details.
  13.  *
  14.  *  You should have received a copy of the GNU General Public License
  15.  *  along with this program; if not, write to the Free Software
  16.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  *
  18.  */
  19. // $_FILEHEADER_BEGIN ****************************
  20. // 文件名称:CriticalSection.h
  21. // 创建日期:20050404
  22. // 创建人: 王耿
  23. // 文件说明:互斥对象
  24. // 0.01 王耿 20050403
  25. // 创建文件
  26. // $_FILEHEADER_END ******************************
  27. #ifndef __CRISECTION_H
  28. #define __CRISECTION_H
  29. namespace UC
  30. {
  31. #ifdef WIN32
  32.     
  33. #include <Windows.h>
  34.     
  35. #else
  36.     
  37. #include <pthread.h>
  38. #include <unistd.h>
  39. #endif//_XNIX
  40.     
  41.     
  42.     class CCriticalSection
  43.     {
  44.     private:
  45.         //windows OS
  46. #ifdef WIN32
  47.         CRITICAL_SECTION moSection;
  48. #else
  49.         pthread_mutex_t mMutex;
  50. #endif
  51.         
  52.         
  53.     public:
  54.         CCriticalSection()
  55.         {
  56. #ifdef WIN32
  57.             InitializeCriticalSection(&moSection);
  58. #else
  59.             mMutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
  60.             //pthread_mutex_init(&mMutex,NULL);
  61. #endif
  62.         };
  63.         ~CCriticalSection()
  64.         {
  65. #ifdef WIN32
  66.             DeleteCriticalSection(&moSection);
  67. #else
  68.             pthread_mutex_destory(&mMutex);
  69. #endif
  70.         }
  71.         void  inline Enter()
  72.         {
  73. #ifdef WIN32
  74.             EnterCriticalSection(&moSection);
  75. #else
  76.             pthread_mutex_lock(&mMutex);
  77. #endif
  78.         }
  79.         void inline Leave()
  80.         {
  81. #ifdef WIN32
  82.             LeaveCriticalSection(&moSection);
  83. #else
  84.             pthread_mutex_unlock(&mMutex);
  85. #endif
  86.         };
  87.         
  88.     };
  89.     class CCriticalSectionHelper
  90.     {
  91.     public:
  92.         CCriticalSectionHelper(CCriticalSection& aCriticalSection)
  93.             :moCriticalSection(aCriticalSection)
  94.         {
  95.             moCriticalSection.Enter();
  96.         };
  97.         ~CCriticalSectionHelper(){
  98.             moCriticalSection.Leave();
  99.         }
  100.     private:
  101.         CCriticalSection& moCriticalSection;
  102.     };
  103. };
  104. #endif // __CRISECTION_H