wsTimerThread.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @brief
  3. websocket定时发送线程类
  4. * @version
  5. V 1.0.0
  6. * @author
  7. 王益俊
  8. * @date
  9. 创建时间: 2018-08-17\n
  10. * @note
  11. 2018-08-17 创建类。\n
  12. * @warning
  13. * @bug
  14. */
  15. #pragma once
  16. #include <boost/thread.hpp>
  17. #include <boost/bind.hpp>
  18. #include <boost/atomic.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/thread/condition.hpp>
  21. #include <boost/serialization/singleton.hpp>
  22. #include "thread_safe_map.h"
  23. #include "ws_common.h"
  24. namespace YA
  25. {
  26. class wsTimerThread
  27. {
  28. private:
  29. boost::shared_ptr<boost::thread> __Thread;//线程
  30. boost::atomic<bool> __Enable;//线程是否继续运行的标志
  31. boost::atomic<bool> __Running;//线程是否正在运行的标志
  32. boost::condition_variable_any __ExitCond;//条件变量
  33. boost::mutex __ExitMutex;//锁(配合__ExitCond)
  34. _THREAD_CONFIG_ __Config;//线程配置
  35. thread_safe_map<int, _CARD_POS_> __CardPosList;//卡位置列表
  36. private:
  37. /**
  38. * @brief
  39. 重置函数。
  40. * @param 无\n
  41. * @return 无\n
  42. * @note
  43. * @warning
  44. * @bug
  45. */
  46. void __Reset();
  47. /**
  48. * @brief
  49. 检查线程配置是否合法函数。
  50. * @param 无\n
  51. * @return 无\n
  52. * @note
  53. * @warning
  54. * @bug
  55. */
  56. void __ChkConfig();
  57. protected:
  58. /**
  59. * @brief
  60. 线程函数。
  61. * @param [in] wsTimerThread* pOwner 线程所属者\n
  62. * @return 无\n
  63. * @note
  64. * @warning
  65. * @bug
  66. */
  67. void _ThreadFunc( wsTimerThread* pOwner );
  68. public:
  69. wsTimerThread();
  70. ~wsTimerThread();
  71. /**
  72. * @brief
  73. 启动线程函数。
  74. * @param 无\n
  75. * @return 无\n
  76. * @note
  77. * @warning
  78. * @bug
  79. */
  80. void Start();
  81. /**
  82. * @brief
  83. 停止线程函数。
  84. * @param 无\n
  85. * @return 无\n
  86. * @note
  87. * @warning
  88. * @bug
  89. */
  90. void Stop();
  91. /**
  92. * @brief
  93. 初始化函数。
  94. * @param [in] const _THREAD_CONFIG_& Config 线程配置\n
  95. * @return 无\n
  96. * @note
  97. * @warning
  98. * @bug
  99. */
  100. void Init( const _THREAD_CONFIG_& Config );
  101. /**
  102. * @brief
  103. 添加卡位置函数。
  104. * @param [in] const _CARD_POS_& pos 卡位置结构体\n
  105. * @return 无\n
  106. * @note
  107. * @warning
  108. * @bug
  109. */
  110. void add_card_pos( const _CARD_POS_& pos );
  111. /**
  112. * @brief
  113. 更新卡位置函数。
  114. * @param [in] const _CARD_POS_& pos 卡位置结构体\n
  115. * @return 无\n
  116. * @note
  117. * @warning
  118. * @bug
  119. */
  120. void upt_card_pos( const _CARD_POS_& pos );
  121. /**
  122. * @brief
  123. 删除卡位置函数。
  124. * @param [in] const _CARD_POS_& pos 卡位置结构体\n
  125. * @return 无\n
  126. * @note
  127. * @warning
  128. * @bug
  129. */
  130. void del_card_pos( const _CARD_POS_& pos );
  131. };
  132. }
  133. //单件定义
  134. typedef boost::serialization::singleton<YA::wsTimerThread> singleton_wsTimerThread;
  135. #define swsTimerThrd singleton_wsTimerThread::get_mutable_instance()
  136. #define swsTimerThrd_const singleton_wsTimerThread::get_const_instance()