Semaphore.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // "$Id: Semaphore.h 121830 2013-06-15 01:38:33Z sun_xiaohui $"
  3. //
  4. // Copyright (c)1992-2007, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description:
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. #ifndef __INFRA3_SEMAPHORE_H__
  11. #define __INFRA3_SEMAPHORE_H__
  12. #include "Defs.h"
  13. namespace Dahua{
  14. namespace Infra{
  15. struct SemaphoreInternal;
  16. /// \class CSemaphore
  17. /// \brief 多平台信号量类
  18. class INFRA_API CSemaphore
  19. {
  20. CSemaphore(CSemaphore const&);
  21. CSemaphore& operator=(CSemaphore const&);
  22. public:
  23. /// 构造函数,会创建系统信号量
  24. /// \param initialCount 信号量初始计数
  25. explicit CSemaphore(int initialCount = 0);
  26. /// 析构函数,会销毁系统互斥量
  27. ~CSemaphore();
  28. /// 减少信号量计数,如果已经减少到0,会阻塞调用的线程
  29. /// \return 当前信号量计数
  30. int pend();
  31. /// 增加信号量计数,如果是从0累加,会唤醒其等待队列的第一个线程
  32. /// \return 当前信号量计数
  33. int post();
  34. /// 减少信号量计数,如果已经减少到0,会阻塞调用的线程, 直到超时
  35. /// \param timeout 超时时间,单位:毫秒(ms)
  36. /// \return 当前信号量计数, 返回-1表示超时
  37. int pend(uint32_t timeout);
  38. /// 尝试减少信号量,如果信号量已经为0,则马上返回
  39. /// \return 0 表示信号量减少成功,-1表示信号量减少失败
  40. /// 通过getLastError()查看错误号
  41. int tryPend();
  42. private:
  43. SemaphoreInternal* m_internal;
  44. };
  45. } // namespace Infra
  46. } // namespace Dahua
  47. #endif //__INFRA_SEMAPHORE_H__
  48. //
  49. // End of "$Id: Semaphore.h 121830 2013-06-15 01:38:33Z sun_xiaohui $"
  50. //