Chinaunix首页 | 论坛 | 博客
  • 博客访问: 383220
  • 博文数量: 82
  • 博客积分: 1855
  • 博客等级: 上尉
  • 技术积分: 846
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-12 12:28
文章存档

2013年(3)

2012年(8)

2011年(71)

分类: LINUX

2011-11-13 23:17:58

  1. lock.h

  2. #ifndef __LOCK_H_
  3. #define __LOCK_H_

  4. #include <pthread.h>

  5. //互斥锁类
  6. class CMutex
  7. {
  8. public:
  9.  typedef pthread_mutex_t Mutex_T;
  10.  typedef pthread_mutexattr_t Mattr_T;
  11.  typedef int MType_T;

  12. public:
  13.  CMutex();
  14.  CMutex(MType_T type_t);
  15.  ~CMutex();

  16. public:
  17.  int Lock()
  18.  {
  19.   return pthread_mutex_lock(&m_mutex);
  20.  }

  21.  int Unlock()
  22.  {
  23.   return pthread_mutex_unlock(&m_mutex);
  24.  }

  25.  int Trylock()
  26.  {
  27.   return pthread_mutex_trylock(&m_mutex);
  28.  }

  29.  Mutex_T& GetMutex()
  30.  {
  31.   return m_mutex;
  32.  }

  33. private:
  34.   Mutex_T m_mutex;
  35.   Mattr_T m_attr;
  36. };

  37. class CMAutoLock //对线程互斥锁的控制类
  38. {
  39. public:
  40.  explicit CMAutoLock(CMutex& mutex) : m_lock(mutex)
  41.  {
  42.   m_lock.Lock();
  43.  }
  44.  ~CMAutoLock()
  45.  {
  46.   m_lock.Unlock();
  47.  }

  48. private:
  49.  CMutex& m_lock;

  50. private:
  51.  CMAutoLock operator=(const CMAutoLock& mutex);
  52.  CMAutoLock(const CMAutoLock& mutex)
  53. };

  54. //读写锁类
  55. class CRWLock
  56. {
  57. public:
  58.  typedef pthread_rwlock_t RWLock_T;

  59. public:
  60.  CRWLock();

  61.  ~CRWLock();

  62. public:
  63.  int RLock()
  64.  {
  65.   return pthread_rwlock_rdlock(&m_rwlock);
  66.  }

  67.  int WLock()
  68.  {
  69.   return pthread_rwlock_wrlock(&m_rwlock);
  70.  }

  71.  int TryRLock()
  72.  {
  73.   return pthread_rwlock_tryrdlock(&m_rwlock);
  74.  }

  75.  int TryWLock()
  76.  {
  77.   return pthread_rwlock_trywrlock(&m_rwlock);
  78.  }

  79.  int Unlock()
  80.  {
  81.   return pthread_rwlock_unlock(&m_rwlock);
  82.  }

  83. private:
  84.  RWLock_T m_rwlock;
  85. };

  86. class CCond //条件变量类
  87. {
  88. public:
  89.  typedef pthread_cond_t Cond_T;

  90. public:
  91.  CCond();
  92.  ~CCond();

  93. public:
  94.  int Lock()
  95.  {
  96.   return m_lock.Lock();
  97.  }

  98.  int Unlock()
  99.  {
  100.   return m_lock.Unlock();
  101.  }

  102.  int Trylock()
  103.  {
  104.   return m_lock.Trylock();
  105.  }

  106.  bool Wait()
  107.  {
  108.   return 0 == pthread_cond_wait(&m_cond, &m_lock.GetMutex());
  109.  }

  110.  bool Wait( long int sencond ); //此函数非内联

  111.  void Signal()
  112.  {
  113.   pthread_cond_signal(&m_cond);
  114.  }

  115.  void Broadcast()
  116.  {
  117.   pthread_cond_broadcast(&m_cond);
  118.  }

  119. private:
  120.  Cond_T m_cond;
  121.  CMutex m_lock; //互斥锁
  122. };

  123. #endif
  1. lock.cpp


  2. #include "lock.h"

  3. #include <sys/time.h>

  4. //互斥锁类
  5. CMutex::CMutex()
  6. {
  7.  pthread_mutexattr_init(&m_attr);
  8.  pthread_mutexattr_settype(&m_attr,PTHREAD_MUTEX_NORMAL);
  9.  pthread_mutex_init(&m_mutex,&m_attr);
  10. }
  11. CMutex::CMutex(MType_T type_t)
  12. {
  13.  pthread_mutexattr_init(&m_attr);
  14.  pthread_mutexattr_settype(&m_attr,type_t);
  15.  pthread_mutex_init(&m_mutex,&m_attr);
  16. }
  17. CMutex::~CMutex()
  18. {
  19.  pthread_mutex_destroy(&m_mutex);
  20.  pthread_mutexattr_destroy(&m_attr);
  21. }

  22. //读写锁类
  23. CRWLock::CRWLock()
  24. {
  25.  pthread_rwlock_init(&m_rwlock,NULL);
  26. }
  27. CRWLock::~CRWLock()
  28. {
  29.  pthread_rwlock_destroy(&m_rwlock);
  30. }

  31. //条件变量类
  32. CCond::CCond()
  33. {
  34.  pthread_cond_init(&m_cond,NULL);
  35. }

  36. CCond::~CCond()
  37. {
  38.  pthread_cond_destroy(&m_cond);
  39. }

  40. bool CCond::Wait( long int sencond )
  41. {
  42.  timespec tsp;
  43.  struct timeval now;
  44.  gettimeofday( &now, NULL );
  45.  sencond += now.tv_sec;
  46.  tsp.tv_sec = now.tv_sec + sencond;
  47.  tsp.tv_nsec = now.tv_usec*1000 ;

  48.  return 0 == pthread_cond_timedwait( &m_cond, &m_lock.GetMutex(), &tsp );
  49. }
阅读(2415) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~