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. // 
  21. // Copyright (C) Sina Corporation.  All Rights Reserved
  22. // 文件名称:CriticalSection.h
  23. // 创建日期:20050404
  24. // 创建人: 王耿
  25. // 文件说明:互斥对象
  26. // 0.01 王耿 20050403
  27. // 创建文件
  28. // $_FILEHEADER_END ******************************
  29. #ifndef __CRISECTION_H
  30. #define __CRISECTION_H
  31. namespace UC
  32. {
  33. #ifdef WIN32
  34.     
  35. #include <Windows.h>
  36.     
  37. #else
  38.     
  39. #include <pthread.h>
  40. #include <unistd.h>
  41. #endif//_XNIX
  42.     
  43.     
  44.     class CCriticalSection
  45.     {
  46.     private:
  47.         //windows OS
  48. #ifdef WIN32
  49.         CRITICAL_SECTION moSection;
  50. #else
  51.         pthread_mutex_t mMutex;
  52. #endif
  53.         
  54.         
  55.     public:
  56.         CCriticalSection()
  57.         {
  58. #ifdef WIN32
  59.             InitializeCriticalSection(&moSection);
  60. #else
  61.             mMutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
  62.             //pthread_mutex_init(&mMutex,NULL);
  63. #endif
  64.         };
  65.         ~CCriticalSection()
  66.         {
  67. #ifdef WIN32
  68.             DeleteCriticalSection(&moSection);
  69. #else
  70.             pthread_mutex_destory(&mMutex);
  71. #endif
  72.         }
  73.         void  inline Enter()
  74.         {
  75. #ifdef WIN32
  76.             EnterCriticalSection(&moSection);
  77. #else
  78.             pthread_mutex_lock(&mMutex);
  79. #endif
  80.         }
  81.         void inline Leave()
  82.         {
  83. #ifdef WIN32
  84.             LeaveCriticalSection(&moSection);
  85. #else
  86.             pthread_mutex_unlock(&mMutex);
  87. #endif
  88.         };
  89.         
  90.     };
  91.     class CCriticalSectionHelper
  92.     {
  93.     public:
  94.         CCriticalSectionHelper(CCriticalSection& aCriticalSection)
  95.             :moCriticalSection(aCriticalSection)
  96.         {
  97.             moCriticalSection.Enter();
  98.         };
  99.         ~CCriticalSectionHelper(){
  100.             moCriticalSection.Leave();
  101.         }
  102.     private:
  103.         CCriticalSection& moCriticalSection;
  104.     };
  105. };
  106. #endif // __CRISECTION_H