1
0

module_const.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef MODULE_CONST_H
  2. #define MODULE_CONST_H
  3. /**
  4. * @brief 包含常用的常量,接口基类,工具类
  5. * @author 戴月腾
  6. * @date 2018-08-24
  7. */
  8. #include<map>
  9. #include<vector>
  10. #include<string>
  11. #include<chrono>
  12. #include"db_api/CDBConnPool.h"
  13. #include"log.h"
  14. #include<rapidjson/document.h>
  15. #include<rapidjson/prettywriter.h>
  16. #include<rapidjson/stringbuffer.h>
  17. #include<websocket/wsClientMgr.h>
  18. #include<thread>
  19. #include "websocket/constdef.h"
  20. #include"card.h"
  21. #include"area.h"
  22. #include"ya_event.h"
  23. #include"config_file.h"
  24. /**
  25. * @brief 线程接口类,重写run函数,改变睡眠时间 sleep_ms
  26. */
  27. class i_thread
  28. {
  29. public:
  30. i_thread()
  31. {
  32. sleep_ms=5*1000;
  33. }
  34. virtual ~i_thread(){}
  35. /**
  36. * @brief 启动线程
  37. */
  38. void start()
  39. {
  40. _thread_flag=true;
  41. _thread_handler=boost::thread(&i_thread::thread_proc, this);
  42. }
  43. /**
  44. * @brief 终止线程
  45. */
  46. void stop()
  47. {
  48. _thread_flag=false;
  49. _thread_handler.interrupt();
  50. }
  51. ///线程睡眠时间 毫秒
  52. std::atomic<int> sleep_ms;
  53. protected:
  54. /// 互斥量
  55. std::mutex _mutex;
  56. /**
  57. * @brief 线程函数
  58. */
  59. virtual void run(){}
  60. private:
  61. /// 线程句柄
  62. boost::thread _thread_handler;
  63. /// 线程标志
  64. std::atomic<bool> _thread_flag;
  65. void thread_proc()
  66. {
  67. while(_thread_flag)
  68. {
  69. try
  70. {
  71. run();
  72. boost::this_thread::sleep_for(boost::chrono::milliseconds(sleep_ms));
  73. }
  74. catch (boost::thread_interrupted&)
  75. {
  76. }
  77. catch(std::exception&)
  78. {
  79. log_error("thread_proc exception 结束线程 i_thread");
  80. }
  81. }
  82. }
  83. };
  84. template<typename T>
  85. class singleton_base
  86. {
  87. public:
  88. static T *instance()
  89. {
  90. if(nullptr != _instance)
  91. {
  92. return _instance;
  93. }
  94. std::lock_guard<std::mutex> ll(_mutex_singleton_base);
  95. if(nullptr == _instance)
  96. {
  97. _instance = new(std::nothrow) T();
  98. }
  99. return _instance;
  100. }
  101. protected:
  102. //使继承者无法public构造函数和析构函数
  103. singleton_base(){}
  104. //virtual ~singleton_base(){}
  105. private:
  106. //禁止拷贝构造和赋值运算符
  107. singleton_base(const singleton_base&){}
  108. singleton_base &operator=(const singleton_base&){}
  109. //它的唯一工作就是在析构函数中析构Singleton的实例,所以private
  110. class Garbo
  111. {
  112. public:
  113. ~Garbo()
  114. {
  115. if (singleton_base::_instance)
  116. {
  117. delete singleton_base::_instance;
  118. singleton_base::_instance = nullptr;
  119. }
  120. }
  121. };
  122. //定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数,我们不需要访问这个变量,所以不需要初始化
  123. static Garbo garbo;
  124. static T *_instance;
  125. static std::mutex _mutex_singleton_base;
  126. };
  127. template<typename T>
  128. T *singleton_base<T>::_instance = nullptr;
  129. template<typename T>
  130. std::mutex singleton_base<T>::_mutex_singleton_base;
  131. #endif // MODULE_CONST_H