Runnable.h
int main()
{
char ThreadName[512] = "daxianyuThread_1";
FMyRunnable(EThreadPriority::TPri_Normal, 0, ThreadName);
Sleep(100000);
return 0;
}
class FMyRunnable : public FRunnable
{
public:
FMyRunnable(FRunnable* InRunable,
const EThreadPriority InPriority /*= EThreadPriority::TPri_Normal*/,
unsigned int InStack /*= 0*/,
const char* InThreadName)
:bStop(false)
{
Thread = Platform::Create(this,InPriority, InStack, InThreadName);
}
virtual bool Init()
{
return true;
};
virtual int Run()
{
for (;;)
{
while (!bStop) //逻辑层
{
//具体逻辑
Sleep(200);
std::cout << "FMyRunnable" << std::endl;
}
Sleep(1000);
break;
}
return 0;
}
virtual int Stop()
{
return 0;
};
virtual bool Exit()
{
delete Thread;
return true;
}
virtual FRunnableThread* GetThread()
{
return Thread;
}
protected:
bool bStop;
FRunnableThread* Thread;
};
class FRunnable
{
public:
virtual bool Init() { return false; };
virtual int Run() = 0;
virtual int Stop() { return 0; };
virtual bool Exit() { return false; };
virtual FRunnableThread* GetThread() = 0;
};
RunnableThread.h
enum EThreadPriority
{
TPri_Normal,
TPri_AboveNormal,
TPri_BelowNormal,
TPri_Highest,
TPri_Lowest,
TPri_SlightlyBelowNormal,
TPri_TimeCritical,
TPri_Num,
};
class FRunnable;
class FRunnableThread
{
public:
FRunnableThread();
virtual bool Create(FRunnable* InRunable) = 0;
void SetPriority(EThreadPriority InPriority);
void SetThreadName(const char* InThreadName);
void SetThreadID(unsigned int InThreadID);
const EThreadPriority GetPriority()const;
const char* GetThreadName() const;
const unsigned int GetThreadID() const;
protected:
EThreadPriority Priority;
char ThreadName[512];
unsigned int ThreadID;
};
RunnableThread.cpp
FRunnableThread::FRunnableThread()
:Priority(EThreadPriority::TPri_Normal)
, ThreadID(0u)
{
memset(ThreadName, 0, 512);
}
void FRunnableThread::SetPriority(EThreadPriority InPriority)
{
Priority = InPriority;
}
void FRunnableThread::SetThreadName(const char* InThreadName)
{
memcpy(ThreadName, InThreadName, 512);
}
void FRunnableThread::SetThreadID(unsigned int InThreadID)
{
ThreadID = InThreadID;
}
const EThreadPriority FRunnableThread::GetPriority() const
{
return Priority;
}
const char* FRunnableThread::GetThreadName() const
{
return ThreadName;
}
const unsigned int FRunnableThread::GetThreadID() const
{
return ThreadID;
}
Platform.h
class Platform
{
public:
static FRunnableThread* Create(
FRunnable* InRunable,
const EThreadPriority InPriority /*= EThreadPriority::TPri_Normal*/,
unsigned int InStack /*= 0*/,
const char* InThreadName = nullptr);
};
Platform.cpp
#if _linux
#include <pthread.h>
#endif
class FWin32RunnableThread : public FRunnableThread
{
public:
virtual bool Create(FRunnable* InRunable);
protected:
HANDLE h;
};
class FlinuxRunnableThread : public FRunnableThread
{
public:
virtual bool Create(FRunnable* InRunable);
};
bool FWin32RunnableThread::Create(FRunnable* InRunable)
{
if (InRunable)
{
auto RunThread = [](LPVOID lpPram)->DWORD
{
FRunnable* Runnable = (FRunnable*)lpPram;
FRunnableThread* MyThread = Runnable->GetThread();
MyThread->SetThreadID(GetCurrentThreadId());
#if _WIN32
struct FTagThreadNameInfo
{
DWORD dwType; //0x1000 4096
LPCSTR szName;
DWORD dwThreadID;
DWORD dwFlags;
};
FTagThreadNameInfo Info;
Info.dwType = 0x1000;
Info.szName = MyThread->GetThreadName();
Info.dwThreadID = MyThread->GetThreadID();
Info.dwFlags = 0;
__try
{
RaiseException(0x406D1388, 0, sizeof(Info) / sizeof(DWORD), (DWORD*)(&Info));
}
__except (EXCEPTION_CONTINUE_EXECUTION)
{
std::cout << "EXCEPTION_CONTINUE_EXECUTION" << std::endl;
}
#elif _linux
//linux //修改名字
#endif
DWORD ReturnValue = Runnable->Run();
Runnable->Exit();
return ReturnValue;
};
HANDLE h = CreateThread(nullptr, 0, RunThread, nullptr, 0, nullptr);
return true;
}
return false;
}
FRunnableThread* Platform::Create(FRunnable* InRunable,
const EThreadPriority InPriority /*= EThreadPriority::TPri_Normal*/,
unsigned int InStack /*= 0*/,
const char* InThreadName)
{
FRunnableThread* InThread = nullptr;
if(InRunable)
{
#if _WIN32
if(InRunable->Init())
{
InThread = new FWin32RunnableThread();
//初始化
InThread->SetPriority(InPriority);
InThread->SetThreadName(InThreadName);
//执行
InThread->Create(InRunable);
}
#elif _linux
InThread = new FlinuxRunnableThread();
#endif
}
return InThread;
}