#ifndef __CCThread__ #define __CCThread__ #include "../../basic/CC.h" // 禁用Visual Studio的警告信息 #pragma warning(disable : 4996) // 定义引用封装宏 #define C_ARG(A) std::ref(A) // 定义线程相关的类型别名 #define CC_Thread void namespace CTL { class IntSleep { public: IntSleep() : interrupt_flag(false) {} void SleepHour(const std::chrono::hours duration) { interrupt_flag = false; std::unique_lock lock(mutex_); condition_.wait_for(lock, duration, [this] { return interrupt_flag; }); } void SleepMinutes(const std::chrono::minutes duration) { interrupt_flag = false; std::unique_lock lock(mutex_); condition_.wait_for(lock, duration, [this] { return interrupt_flag; }); } void SleepSecond(const std::chrono::seconds duration) { interrupt_flag = false; std::unique_lock lock(mutex_); condition_.wait_for(lock, duration, [this] { return interrupt_flag; }); } void SleepMillisecond(const std::chrono::milliseconds duration) { interrupt_flag = false; std::unique_lock lock(mutex_); condition_.wait_for(lock, duration, [this] { return interrupt_flag; }); } void SleepMicrosecond(const std::chrono::microseconds duration) { interrupt_flag = false; std::unique_lock lock(mutex_); condition_.wait_for(lock, duration, [this] { return interrupt_flag; }); } void interrupt() { { std::lock_guard lock(mutex_); interrupt_flag = true; } condition_.notify_all(); } private: std::condition_variable condition_; std::mutex mutex_; bool interrupt_flag; }; class Thread_V { public: virtual ~Thread_V() = default; void Start(); void Stop(); void Wait(); bool Sign() const; virtual void Run() = 0; private: void running(); bool Flag = false, Flag_m = false; std::thread Thread_m; }; /** * @class Thread * @brief 线程管理类,用于创建和控制线程 */ class Thread { public: // 默认构造函数 Thread() = default; Thread(const Thread&& other); // 移动构造函数 Thread(const Thread& other); /** * @brief 构造函数,用于创建线程 * * @tparam Func 要在线程中执行的函数类型 * @tparam Args 函数的参数类型 * @param fun 要在线程中执行的函数 * @param args 函数的参数 */ template explicit Thread(Func&& fun, Args&&... args); // 析构函数 ~Thread(); /** * @brief 设置线程要执行的任务 * * @tparam Func 要在线程中执行的函数类型 * @tparam Args 函数的参数类型 * @param fun 要在线程中执行的函数 * @param args 函数的参数 * @return true 如果设置成功 * @return false 如果线程已经开始执行 */ template bool SetThread(Func&& fun, Args&&... args); /** * @brief 启动线程并执行任务 * * @tparam Func 要在线程中执行的函数类型 * @tparam Args 函数的参数类型 * @param fun 要在线程中执行的函数 * @param args 函数的参数 * @return true 如果线程成功启动 * @return false 如果线程已经开始执行 */ template bool Start(Func &&fun, Args &&...args); /** * @brief 启动线程并执行任务,任务完成后线程自动分离 */ void Start(){ if (task) { Flag = true; Thread_m = std::thread(task); Thread_m.detach(); } } /** * @brief 启动线程并执行任务,等待任务完成 */ void RunWait() { if (task) { Flag = true; Thread_m = std::thread(task); Thread_m.join(); } } // 停止线程 void Stop(); /** * @brief 检查线程是否正在执行任务 * @return true 如果线程正在执行任务 * @return false 如果线程没有执行任务 */ [[nodiscard]] bool Sign() const; /** * @brief 线程休眠 * @param us 休眠时间,单位为微秒 * @return 实际休眠时间,单位为微秒 */ static long Sleep(unsigned int us); /** * @brief 线程休眠 * @param MS 休眠时间,单位为毫秒 * @return 实际休眠时间,单位为毫秒 */ static long SleepMS(unsigned int MS); /** * @brief 获取线程对象的引用 * @return std::thread& 线程对象的引用 */ std::thread& GetThread(); /** * @brief 获取线程的标识符 * @return std::thread::id 线程的标识符 */ [[nodiscard]] std::thread::id GetThreadId() const; protected: private: std::thread Thread_m; // 线程对象 std::function task; // 线程任务 bool Flag = false; // 线程状态标志 }; } /** * @brief 构造函数模板,用于创建线程并执行给定的任务 * * @tparam Func 要在线程中执行的函数类型 * @tparam Args 函数的参数类型 * @param fun 要在线程中执行的函数 * @param args 函数的参数 */ template CTL::Thread::Thread(Func &&fun, Args &&...args) { SetThread(fun, args...); } /** * @brief 设置线程要执行的任务 * * @tparam Func 要在线程中执行的函数类型 * @tparam Args 函数的参数类型 * @param fun 要在线程中执行的函数 * @param args 函数的参数 * @return true 如果设置成功 * @return false 如果线程已经开始执行 */ template bool CTL::Thread::SetThread(Func &&fun, Args &&...args) { if (Thread_m.joinable()) { return false; } task = std::bind(std::forward(fun), std::forward(args)...); return true; } /** * @brief 启动线程并执行任务 * * @tparam Func 要在线程中执行的函数类型 * @tparam Args 函数的参数类型 * @param fun 要在线程中执行的函数 * @param args 函数的参数 * @return true 如果线程成功启动 * @return false 如果线程已经开始执行 */ template bool CTL::Thread::Start(Func &&fun, Args &&...args) { if (Thread_m.joinable()) { return false; } task = std::bind(std::forward(fun), std::forward(args)...); Start(); return true; } #endif