Service_NSSM/CC_SDK/Include/Module/IO/CCThread.h

248 lines
7.9 KiB
C
Raw Normal View History

2025-09-30 16:24:15 +08:00
#ifndef CC_Thread_
#define CC_Thread_
2025-09-27 14:24:18 +08:00
2025-09-30 16:24:15 +08:00
#include "CC.h"
#include "TL/Map.h"
2025-09-27 14:24:18 +08:00
// 禁用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<std::mutex> lock(mutex_);
condition_.wait_for(lock, duration, [this] { return interrupt_flag; });
}
void SleepMinutes(const std::chrono::minutes duration) {
interrupt_flag = false;
std::unique_lock<std::mutex> lock(mutex_);
condition_.wait_for(lock, duration, [this] { return interrupt_flag; });
}
void SleepSecond(const std::chrono::seconds duration) {
interrupt_flag = false;
std::unique_lock<std::mutex> lock(mutex_);
condition_.wait_for(lock, duration, [this] { return interrupt_flag; });
}
void SleepMillisecond(const std::chrono::milliseconds duration) {
interrupt_flag = false;
std::unique_lock<std::mutex> lock(mutex_);
condition_.wait_for(lock, duration, [this] { return interrupt_flag; });
}
void SleepMicrosecond(const std::chrono::microseconds duration) {
interrupt_flag = false;
std::unique_lock<std::mutex> lock(mutex_);
condition_.wait_for(lock, duration, [this] { return interrupt_flag; });
}
void interrupt() {
{
std::lock_guard<std::mutex> lock(mutex_);
interrupt_flag = true;
}
condition_.notify_all();
}
private:
std::condition_variable condition_;
std::mutex mutex_;
bool interrupt_flag;
};
2025-09-30 16:24:15 +08:00
class Thread_Integration {
2025-09-27 14:24:18 +08:00
public:
2025-09-30 16:24:15 +08:00
virtual ~Thread_Integration() = default;
2025-09-27 14:24:18 +08:00
void Start();
void Stop();
void Wait();
2025-09-30 16:24:15 +08:00
[[nodiscard]] bool Sign() const;
2025-09-27 14:24:18 +08:00
virtual void Run() = 0;
private:
void running();
bool Flag = false, Flag_m = false;
std::thread Thread_m;
};
/**
* @class Thread
* @brief 线线
*/
2025-09-30 16:24:15 +08:00
class Thread{
2025-09-27 14:24:18 +08:00
public:
// 默认构造函数
2025-09-30 16:24:15 +08:00
Thread();
Thread(const Thread&& other) noexcept ;
2025-09-27 14:24:18 +08:00
// 移动构造函数
Thread(const Thread& other);
/**
* @brief 线
*
* @tparam Func 线
* @tparam Args
* @param fun 线
* @param args
*/
template <typename Func, typename... Args>
explicit Thread(Func&& fun, Args&&... args);
// 析构函数
~Thread();
/**
* @brief 线
*
* @tparam Func 线
* @tparam Args
* @param fun 线
* @param args
* @return true
* @return false 线
*/
template <typename Func, typename... Args>
bool SetThread(Func&& fun, Args&&... args);
/**
* @brief 线
*
* @tparam Func 线
* @tparam Args
* @param fun 线
* @param args
* @return true 线
* @return false 线
*/
template <typename Func, typename... Args>
bool Start(Func &&fun, Args &&...args);
/**
2025-09-30 16:24:15 +08:00
* @brief 线线
2025-09-27 14:24:18 +08:00
*/
void Start(){
2025-09-30 16:24:15 +08:00
if (task && !Flag) {
Flag = true;
Thread_m = new std::thread(task);
}
}
/**
* @brief 线线
*/
void StartDetach() {
if (task && !Flag) {
2025-09-27 14:24:18 +08:00
Flag = true;
2025-09-30 16:24:15 +08:00
Thread_m = new std::thread(task);
Thread_m->detach();
2025-09-27 14:24:18 +08:00
}
}
/**
* @brief 线
*/
void RunWait() {
2025-09-30 16:24:15 +08:00
if (task && !Flag) {
2025-09-27 14:24:18 +08:00
Flag = true;
2025-09-30 16:24:15 +08:00
Thread_m = new std::thread(task);
Thread_m->join();
2025-09-27 14:24:18 +08:00
}
}
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& 线
*/
2025-09-30 16:24:15 +08:00
[[nodiscard]] std::thread* GetThread() const;
2025-09-27 14:24:18 +08:00
/**
* @brief 线
* @return std::thread::id 线
*/
[[nodiscard]] std::thread::id GetThreadId() const;
2025-09-30 16:24:15 +08:00
/**
* @brief 线退
* @param Flag 退
*/
static void SetSafeExit(const bool Flag);
2025-09-27 14:24:18 +08:00
protected:
2025-09-30 16:24:15 +08:00
inline static std::mutex thread_mutex_t;
inline static Map<int,Thread*> CC_thread_Map_t;
inline static bool SafeExit = true;
static int AssignID();
int threadID = -1;
std::thread* Thread_m = nullptr; // 线程对象
std::function<void()> task{}; // 线程任务
2025-09-27 14:24:18 +08:00
bool Flag = false; // 线程状态标志
};
}
/**
* @brief 线
*
* @tparam Func 线
* @tparam Args
* @param fun 线
* @param args
*/
template<typename Func, typename ... Args>
CTL::Thread::Thread(Func &&fun, Args &&...args) {
2025-09-30 16:24:15 +08:00
std::unique_lock lock(thread_mutex_t);
this->threadID = AssignID();
CC_thread_Map_t.put(threadID, this);
2025-09-27 14:24:18 +08:00
SetThread(fun, args...);
}
/**
* @brief 线
*
* @tparam Func 线
* @tparam Args
* @param fun 线
* @param args
* @return true
* @return false 线
*/
template<typename Func, typename ... Args>
bool CTL::Thread::SetThread(Func &&fun, Args &&...args) {
2025-09-30 16:24:15 +08:00
if (Thread_m && Thread_m->joinable()) {
2025-09-27 14:24:18 +08:00
return false;
}
task = std::bind(std::forward<Func>(fun), std::forward<Args>(args)...);
return true;
}
/**
* @brief 线
*
* @tparam Func 线
* @tparam Args
* @param fun 线
* @param args
* @return true 线
* @return false 线
*/
template<typename Func, typename ... Args>
bool CTL::Thread::Start(Func &&fun, Args &&...args) {
2025-09-30 16:24:15 +08:00
if (Thread_m && Thread_m->joinable()) {
2025-09-27 14:24:18 +08:00
return false;
}
task = std::bind(std::forward<Func>(fun), std::forward<Args>(args)...);
Start();
return true;
}
#endif