123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #ifndef MODULE_CONST_H
- #define MODULE_CONST_H
- #include<map>
- #include<vector>
- #include<string>
- #include<chrono>
- #include"db_api/CDBConnPool.h"
- #include"log.h"
- #include<rapidjson/document.h>
- #include<rapidjson/prettywriter.h>
- #include<rapidjson/stringbuffer.h>
- #include<websocket/wsClientMgr.h>
- #include<thread>
- #include "websocket/constdef.h"
- #include"card.h"
- #include"area.h"
- #include"ya_event.h"
- #include"config_file.h"
- class i_thread
- {
- public:
- i_thread()
- {
- sleep_ms=5*1000;
- }
- virtual ~i_thread(){}
-
- void start()
- {
- _thread_flag=true;
- _thread_handler=boost::thread(&i_thread::thread_proc, this);
- }
-
- void stop()
- {
- _thread_flag=false;
- _thread_handler.interrupt();
- }
-
- std::atomic<int> sleep_ms;
- protected:
-
- std::mutex _mutex;
-
- virtual void run(){}
- private:
-
- boost::thread _thread_handler;
-
- std::atomic<bool> _thread_flag;
- void thread_proc()
- {
- while(_thread_flag)
- {
- try
- {
- run();
- boost::this_thread::sleep_for(boost::chrono::milliseconds(sleep_ms));
- }
- catch (boost::thread_interrupted&)
- {
- }
- catch(std::exception&)
- {
- log_error("thread_proc exception 结束线程 i_thread");
- }
- }
- }
- };
- template<typename T>
- class singleton_base
- {
- public:
- static T *instance()
- {
- if(nullptr != _instance)
- {
- return _instance;
- }
- std::lock_guard<std::mutex> ll(_mutex_singleton_base);
- if(nullptr == _instance)
- {
- _instance = new(std::nothrow) T();
- }
- return _instance;
- }
- protected:
-
- singleton_base(){}
-
- private:
-
- singleton_base(const singleton_base&){}
- singleton_base &operator=(const singleton_base&){}
-
- class Garbo
- {
- public:
- ~Garbo()
- {
- if (singleton_base::_instance)
- {
- delete singleton_base::_instance;
- singleton_base::_instance = nullptr;
- }
- }
- };
-
- static Garbo garbo;
- static T *_instance;
- static std::mutex _mutex_singleton_base;
- };
- template<typename T>
- T *singleton_base<T>::_instance = nullptr;
- template<typename T>
- std::mutex singleton_base<T>::_mutex_singleton_base;
- #endif
|