IPBS_Station/SDK/include/CCThread.h

156 lines
2.3 KiB
C
Raw Normal View History

2024-08-13 17:07:34 +08:00
#ifndef __CCThread__
#define __CCThread__
#include <thread>
#include <functional>
namespace Threading
{
inline void Sleep(unsigned int us)
{
if(us!=0)
std::this_thread::sleep_for(std::chrono::microseconds(us));
else
while (true);
};
};
#define __CPP__
#ifdef __CPP__
#pragma warning(disable : 4996)
#define C_ARG(A) std::ref(A)
#define CC_Thread void
#define FUNA void*
2024-11-01 16:09:31 +08:00
class CCThread:public std::thread
2024-08-13 17:07:34 +08:00
{
public:
CCThread();
CCThread(std::thread a);
CCThread(CCThread&& other);
2024-11-01 16:09:31 +08:00
using std::thread::thread;
2024-08-13 17:07:34 +08:00
~CCThread();
template <typename Func, typename... Args>
bool SetThread(Func&& fun, Args&&... args)
{
if (Thread.joinable()) {
return false;
}
task = std::bind(std::forward<Func>(fun), std::forward<Args>(args)...);
return true;
}
void Start()
{
if (task) {
Flag = true;
Thread = std::thread(task);
Thread.detach();
}
2024-11-01 16:09:31 +08:00
}
void RunWait() {
if (task) {
Flag = true;
Thread = std::thread(task);
Thread.join();
}
2024-08-13 17:07:34 +08:00
}
void Stop();
bool Sign();
2024-11-01 16:09:31 +08:00
static void Sleep(unsigned int us){
if(us!=0)
std::this_thread::sleep_for(std::chrono::microseconds(us));
else{
while (true);
}
};
2024-08-13 17:07:34 +08:00
private:
std::thread Thread;
std::function<void()> task;
bool Flag = false;
};
#elif _WIN32
#include <windows.h>
#define CC_Thread DWORD WINAPI
#define FUNA LPVOID
class CCThread
{
public:
CCThread();
CCThread(CCThread&& other);
bool SetThread(
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ __drv_aliasesMem LPVOID lpParameter = NULL,
_Out_opt_ LPDWORD lpThreadId = nullptr);
bool Start();
bool Stop();
bool Sign();
private:
HANDLE hThread;
DWORD dwaid;
bool Flag = false;
};
#elif __linux__
#include <functional>
#include <pthread.h>
#define CC_Thread void*
#define FUNA void*
typedef void* (*ThreadFuns)(void*);
class CCThread
{
public:
CCThread();
CCThread(CCThread&& other);
bool SetThread(ThreadFuns funs,FUNA arg = NULL);
bool Start();
bool Stop();
bool Sign();
private:
pthread_t Thread;
ThreadFuns Funas;
FUNA agsa;
bool Flag = false;
};
#endif
#endif