// // Created by zhaohe on 19-5-21. // #pragma once #include #include #include #include #include #include #include #include #include #include #include #include // #include "iflytopcpp/core/basic/signal/signal.hpp" #include "iflytopcpp/core/spdlogfactory/logger.hpp" #include "iflytopcpp/core/zexception/zexception.hpp" namespace iflytop { namespace core { using namespace std; class Thread; class Thread { ENABLE_LOGGER(Thread); public: private: string name; pthread_t id = 0; function run; std::mutex lock_; unique_ptr workThread; atomic_bool hasJointd = {false}; atomic_bool exitFlag = {false}; bool waitingForJoin = false; /*当前线程是否运行退出等待回收*/ Singal signal; Thread(std::function run); public: Thread(string name, function run); void wake(); void join(); bool isWaitingForJoin() { return waitingForJoin; } bool getExitFlag() { return exitFlag; } pthread_t getId() const; void sleep() { signal.sleep(); } void sleepForMs(int64_t ms) { signal.sleep_for_us(ms * 1000); } ~Thread(); private: const shared_ptr &getExitException() const; }; class ThisThread { Thread *m_thread = NULL; public: ThisThread(); pthread_t getId() const { return pthread_self(); } bool getExitFlag(); void sleep(); void sleepForMs(int64_t ms); void wake(); }; } // namespace core } // namespace iflytop