//< Simplistic portable thread class. Shutdown signalling left to derived class
// 对线程的一层对象化包装
class Thread
{
private:
ThreadHandle thread;
public:
Thread(){
thread = 0;
}
// 销毁线程
virtual ~Thread(){
if (thread)
CloseHandle(thread);
}
//< Derived class must implement ThreadMain.
// 类运行主函数,重写
virtual void threadMain() = 0;
//< Returns true if thread was successfully created
// 创建一个线程并启动,允许线程主函数threadMain()
bool start(){
DWORD threadId;
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadShim, this, 0, &threadId);
return threadId > 0;
}
// 无期限等待停止线程
void stop(){
if (thread)
WaitForSingleObject(thread, INFINITE);
}
};
static DWORD WINAPI ThreadShim(Thread *instance)
{
STACK_ALIGN(stackAlignMain, instance);
return 0;
}
/* C shim for forced stack alignment */
static void stackAlignMain(Thread *instance)
{
// defer processing to the virtual function implemented in the derived class
instance->threadMain();
}
x265多线程-线程/线程池
最新推荐文章于 2024-10-15 22:39:04 发布