|
楼主 |
发表于 2025-8-27 10:48:47
|
显示全部楼层
AI回答
#ifndef THREAD_MODULE_H
#define THREAD_MODULE_H
#include <windows.h>
#include <thread>
#include <functional>
#include <unordered_map>
#include <mutex>
#include <atomic>
class ThreadModule {
private:
struct ThreadInfo {
HANDLE handle;
std::thread::id stdThreadId;
std::atomic<bool> isRunning;
std::atomic<bool> shouldStop;
ThreadInfo() : handle(NULL), isRunning(false), shouldStop(false) {}
};
static std::unordered_map<DWORD, ThreadInfo> threads;
static std::mutex threadMapMutex;
template<typename Func, typename... Args>
static DWORD WINAPI ThreadWrapper(LPVOID param) {
auto* funcWrapper = static_cast<std::function<void()>*>(param);
DWORD threadId = GetCurrentThreadId();
{
std::lock_guard<std::mutex> lock(threadMapMutex);
if (threads.find(threadId) != threads.end()) {
threads[threadId].isRunning = true;
}
}
(*funcWrapper)();
{
std::lock_guard<std::mutex> lock(threadMapMutex);
if (threads.find(threadId) != threads.end()) {
threads[threadId].isRunning = false;
}
}
delete funcWrapper;
return 0;
}
public:
// 线程开启
template<typename Func, typename... Args>
static DWORD 线程开启(Func&& func, Args&&... args) {
auto* funcWrapper = new std::function<void()>(
std::bind(std::forward<Func>(func), std::forward<Args>(args)...)
);
DWORD threadId;
HANDLE hThread = CreateThread(
NULL, // 默认安全属性
0, // 默认堆栈大小
ThreadWrapper<Func, Args...>, // 线程函数
funcWrapper, // 线程参数
0, // 立即运行
&threadId // 接收线程ID
);
if (hThread != NULL) {
std::lock_guard<std::mutex> lock(threadMapMutex);
ThreadInfo info;
info.handle = hThread;
info.stdThreadId = std::this_thread::get_id();
info.isRunning = true;
info.shouldStop = false;
threads[threadId] = info;
return threadId;
}
delete funcWrapper;
return 0;
}
// 线程关闭
static bool 线程关闭(DWORD 句柄, int 类型 = 0) {
std::lock_guard<std::mutex> lock(threadMapMutex);
auto it = threads.find(句柄);
if (it == threads.end()) {
return false;
}
HANDLE hThread = it->second.handle;
bool result = false;
if (类型 == 0) {
// 安全关闭
it->second.shouldStop = true;
// 等待线程自然结束(最多等待5秒)
DWORD waitResult = WaitForSingleObject(hThread, 5000);
if (waitResult == WAIT_OBJECT_0) {
result = true;
} else {
// 如果线程没有响应,可能需要强制终止
result = (TerminateThread(hThread, 0) != 0);
}
} else if (类型 == 1) {
// 强制关闭
result = (TerminateThread(hThread, 0) != 0);
}
if (result) {
CloseHandle(hThread);
threads.erase(it);
}
return result;
}
// 线程等待
static int 线程等待(DWORD 句柄, int 等待时间) {
std::lock_guard<std::mutex> lock(threadMapMutex);
auto it = threads.find(句柄);
if (it == threads.end()) {
return -2; // 等待失败
}
HANDLE hThread = it->second.handle;
threadMapMutex.unlock(); // 释放锁以避免死锁
DWORD timeout = (等待时间 <= -1) ? INFINITE : static_cast<DWORD>(等待时间);
DWORD result = WaitForSingleObject(hThread, timeout);
switch (result) {
case WAIT_OBJECT_0:
return 0; // 核心对象已被激活
case WAIT_TIMEOUT:
return -1; // 等待超时
case WAIT_ABANDONED:
return -3; // 线程在结束时没有释放核心对象
default:
return -2; // 等待失败
}
}
// 线程暂停
static bool 线程暂停(DWORD 句柄) {
std::lock_guard<std::mutex> lock(threadMapMutex);
auto it = threads.find(句柄);
if (it == threads.end()) {
return false;
}
HANDLE hThread = it->second.handle;
return (SuspendThread(hThread) != (DWORD)-1);
}
// 线程恢复
static bool 线程恢复(DWORD 句柄) {
std::lock_guard<std::mutex> lock(threadMapMutex);
auto it = threads.find(句柄);
if (it == threads.end()) {
return false;
}
HANDLE hThread = it->second.handle;
return (ResumeThread(hThread) != (DWORD)-1);
}
// 线程获取id
static DWORD 线程获取id() {
return GetCurrentThreadId();
}
// 线程获取状态
static bool 线程获取状态(DWORD 句柄) {
std::lock_guard<std::mutex> lock(threadMapMutex);
auto it = threads.find(句柄);
if (it == threads.end()) {
return false;
}
// 检查线程是否仍在运行
DWORD exitCode;
if (GetExitCodeThread(it->second.handle, &exitCode)) {
return (exitCode == STILL_ACTIVE) && it->second.isRunning;
}
return false;
}
// 清理资源(可选)
static void 清理() {
std::lock_guard<std::mutex> lock(threadMapMutex);
for (auto& pair : threads) {
CloseHandle(pair.second.handle);
}
threads.clear();
}
};
// 静态成员定义
std::unordered_map<DWORD, ThreadModule::ThreadInfo> ThreadModule::threads;
std::mutex ThreadModule::threadMapMutex;
#endif // THREAD_MODULE_H |
|