1
0

wsTimerThread.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <ctime>
  23. #include "thread_safe_map.h"
  24. #include "ws_common.h"
  25. #include "jsonBuilder.h"
  26. namespace YA
  27. {
  28. class wsTimerThread
  29. {
  30. private:
  31. boost::shared_ptr<boost::thread> __Thread;//线程
  32. boost::atomic<bool> __Enable;//线程是否继续运行的标志
  33. boost::atomic<bool> __Running;//线程是否正在运行的标志
  34. boost::condition_variable_any __ExitCond;//条件变量
  35. boost::mutex __ExitMutex;//锁(配合__ExitCond)
  36. _THREAD_CONFIG_ __Config;//线程配置
  37. thread_safe_map<int, _CARD_POS_> __CardPosList;//卡位置列表
  38. std::tm __LastSendTime;//上一次发送的时间
  39. jsonBuilder __jsBuilder;//json构造器类
  40. private:
  41. /**
  42. * @brief
  43. 重置函数。
  44. * @param 无\n
  45. * @return 无\n
  46. * @note
  47. * @warning
  48. * @bug
  49. */
  50. void __Reset();
  51. /**
  52. * @brief
  53. 检查线程配置是否合法函数。
  54. * @param 无\n
  55. * @return 无\n
  56. * @note
  57. * @warning
  58. * @bug
  59. */
  60. void __ChkConfig();
  61. /**
  62. * @brief
  63. 发送卡位置函数。
  64. * @param 无\n
  65. * @return 无\n
  66. * @note
  67. * @warning
  68. * @bug
  69. */
  70. void __SendCardPos();
  71. protected:
  72. /**
  73. * @brief
  74. 线程函数。
  75. * @param [in] wsTimerThread* pOwner 线程所属者\n
  76. * @return 无\n
  77. * @note
  78. * @warning
  79. * @bug
  80. */
  81. void _ThreadFunc( wsTimerThread* pOwner );
  82. public:
  83. wsTimerThread();
  84. ~wsTimerThread();
  85. /**
  86. * @brief
  87. 启动线程函数。
  88. * @param 无\n
  89. * @return 无\n
  90. * @note
  91. * @warning
  92. * @bug
  93. */
  94. void Start();
  95. /**
  96. * @brief
  97. 停止线程函数。
  98. * @param 无\n
  99. * @return 无\n
  100. * @note
  101. * @warning
  102. * @bug
  103. */
  104. void Stop();
  105. /**
  106. * @brief
  107. 初始化函数。
  108. * @param [in] const _THREAD_CONFIG_& Config 线程配置\n
  109. * @return 无\n
  110. * @note
  111. * @warning
  112. * @bug
  113. */
  114. void Init( const _THREAD_CONFIG_& Config );
  115. /**
  116. * @brief
  117. 更新卡位置函数。
  118. * @param [in] const _CARD_POS_& pos 卡位置结构体\n
  119. * @return 无\n
  120. * @note
  121. * @warning
  122. * @bug
  123. */
  124. void upt_card_pos( const _CARD_POS_& pos );
  125. /**
  126. * @brief
  127. 删除卡位置函数。
  128. * @param [in] const _CARD_POS_& pos 卡位置结构体\n
  129. * @return 无\n
  130. * @note
  131. * @warning
  132. * @bug
  133. */
  134. void del_card_pos( const _CARD_POS_& pos );
  135. };
  136. }
  137. //单件定义
  138. typedef boost::serialization::singleton<YA::wsTimerThread> singleton_wsTimerThread;
  139. #define swsTimerThrd singleton_wsTimerThread::get_mutable_instance()
  140. #define swsTimerThrd_const singleton_wsTimerThread::get_const_instance()