概述
封装一个简单的线程类,线程函数结束后线程不会自动退出,而是阻塞到了事件函数上,当再次运行该线程时,会触发事件解除阻塞运行函数,而不会重新创建线程。想要使线程退出,必须手动设置线程才会退出。
共有两个基类CThread和CThreadFunc,其中CThread是主要的线程类,它保存线程句柄和线程ID等属性,CThreadFunc是线程处理函数类,处理线程的具体工作。
实现的方法有两种,一种是直接继承CThread,并重写它的Run函数。另一种是先实例化CThread thread,接着创建一个线程函数类继承CThreadFunc,并重写Run函数。然后调用thread的函数AddWorkThread()把线程函数对象添加到线程中
源码
CThread.h
#include <stdio.h>
#include <process.h>
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include "CThreadFunc.h"
class CThread
{
public:
CThread();
~CThread();
//开始线程
bool Start();
//阻塞等待子线程结束
void join(int timeout = -1);
//运行函数
virtual void Run();
//设置线程退出
void SetThreadEnd();
//添加工作线程类
void AddWorkThread(CThreadFunc *cThreadFunc);
//激活线程
void CThread::Resume();
//挂起线程
void CThread::Suspend();
private:
static unsigned int WINAPI StaticThreadFunc(void *arg);
private:
unsigned int m_ThreadID; //线程ID
char *m_Name; //线程名称
HANDLE m_Handle; //线程句柄
bool m_Run; //线程是否正在运行
bool m_isEnd; //是否结束线程
CThreadFunc *m_pThreadFunc; //线程函数类
HANDLE g_hThreadEvent; //事件
};
CThread.cpp
#include "CThread.h"
CThread::CThread()
{
//线程句柄
m_Handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, 0, &m_ThreadID);
m_ThreadID = -1; //线程ID
m_Name = NULL; //线程名称
m_Run = false; //线程是否运行
m_pThreadFunc = NULL; //线程函数类
//初始化事件
g_hThreadEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
}
CThread::~CThread()
{
}
unsigned int CThread::StaticThreadFunc(void *arg)
{
CThread *thread = (CThread *)arg;
while (true)
{
thread->m_Run = false;
WaitForSingleObject(thread->g_hThreadEvent, INFINITE); //等待事件被触发
thread->m_Run = true;
if (thread->m_pThreadFunc != NULL)
{
thread->m_pThreadFunc->Run();
}
else
{
thread->Run(); //线程处理函数
}
if (thread->m_isEnd) //是否推出线程
{
break;
}
}
return 0;
}
//开始线程
bool CThread::Start()
{
//如果线程正在运行则返回
if (m_Run || NULL == m_Handle)
{
return -1;
}
m_Run = true; //重置标志位
SetEvent(g_hThreadEvent); //触发事件
return m_Run;
}
//阻塞等待子线程结束
void CThread::join(int timeout)
{
if (NULL == m_Handle || !m_Run)
{
return;
}
if (timeout <= 0)
{
timeout = INFINITE;
}
//阻塞等待线程结束
::WaitForSingleObject(m_Handle, timeout);
}
//设置线程退出
void CThread::SetThreadEnd()
{
m_isEnd = true;
}
//添加工作线程类
void CThread::AddWorkThread(CThreadFunc *cThreadFunc)
{
m_pThreadFunc = cThreadFunc; //线程函数类
}
//激活线程
void CThread::Resume()
{
if (NULL == m_Handle || !m_Run)
{
return;
}
::ResumeThread(m_Handle);
}
//挂起线程
void CThread::Suspend()
{
if (NULL == m_Handle || m_Run)
{
return;
}
::SuspendThread(m_Handle);
}
//运行函数
void CThread::Run()
{
}
CThreadFunc.h
#pragma once
class CThreadFunc
{
public:
CThreadFunc();
~CThreadFunc();
virtual void Run() = 0;
};
CThreadFunc.cpp
#include "CThreadFunc.h"
CThreadFunc::CThreadFunc()
{
}
CThreadFunc::~CThreadFunc()
{
}
方法一
#include <stdio.h>
#include "CThread.h"
#include "CThreadFunc.h"
class thread : public CThread
{
public:
thread()
{
}
~thread()
{
}
virtual void Run()
{
int i = 5;
while (i)
{
Sleep(1000);
std::cout << "hello world\n";
i--;
}
}
};
int main(void)
{
thread *t = new thread;
t->Start();
t->SetThreadEnd();
t->join();
std::cout << "子线程结束\n";
getchar();
return 0;
}
方法二
#include "CThread.h"
#include "CThreadFunc.h"
class Func :public CThreadFunc
{
public:
virtual void Run()
{
int i = 5;
while (i)
{
Sleep(1000);
std::cout << "object Thread\n";
i--;
}
}
};
int main(void)
{
CThread *t = new thread;
Func *f = new Func;
t->AddWorkThread(f);
t->Start();
t->SetThreadEnd();
t->join();
std::cout << "子线程结束\n";
getchar();
return 0;
}