工作当中频繁使用到多线程,网上参考了一份代码,并根据自己需要改进了一下,贴出来做个记录,以后如果有产生问题还会继续改进。大致介绍下每个类的作用:
1. CThread 线程基类,线程创建及销毁
2. CWorkerThread 工作线程,任务设置获取,池对象设置获取,线程运行及终止
3. CThreadPool 任务运行,空闲线程/忙碌线程管理,销毁线程池
4. CJob 需要执行的任务类,业务逻辑可继承此类
5. CThreadCondition 线程同步,条件变量类,由event触发等待实现
6. CThreadMutex 线程互斥,由临界区实现
7. CThreadManage 暴露给上层的类,主要集中线程池的运行与销毁
以下是各个文件的实现代码,记录一下:
// Thread.h
#ifndef THREAD_H_
#define THREAD_H_
#include "windows.h"
typedef enum _ThreadState
{
THREAD_INI = -1,
THREAD_RUNNING = 0,
THREAD_IDLE = 1,
THREAD_EXIT = 2,
}ThreadState;
class CThread
{
private:
unsigned int m_threadID;
HANDLE m_threadHandle;
char* m_threadName;
ThreadState m_threadState;
BOOL m_isExit;
protected:
static unsigned __stdcall ThreadFunction(void*);
public:
CThread();
virtual ~CThread();
virtual void Run(void) = 0;
BOOL Terminate(void);
BOOL Start(void);
void SetThreadState(ThreadState state) {m_threadState = state;}
ThreadState GetThreadState(void) {return m_threadState;}
void SetThreadName(char* thrName);
char* GetThreadName(void) {return m_threadName;}
BOOL GetExit() {return m_isExit;}
void SetExit(BOOL exit) {m_isExit = exit;}
HANDLE GetThreadHandle() {return m_threadHandle;}
void SetThreadHandle(HANDLE hdl) {m_threadHandle = hdl;}
int GetThreadID(void) {return m_threadID;}
};
#endif //Thread.h
// Thread.cpp
#include "stdio.h" #include <process.h> #include <string.h> #include "Thread.h" CThread::CThread(): m_isExit(FALSE), m_threadHandle(0), m_threadID(0), m_threadName(NULL), m_threadState(THREAD_INIT) { } CThread::~CThread() { if (NULL != m_threadName) { free(m_threadName); m_threadName = NULL; } } BOOL CThread::Terminate() { if (m_isExit) { _endthreadex(0); if (m_threadHandle) CloseHandle(m_threadHandle); return TRUE; } return FALSE; } unsigned __stdcall CThread::ThreadFunction(void* pArg) { CThread* pThread = (CThread*)pArg; pThread->Run(); return 0; } BOOL CThread::Start() { unsigned int threadID = 0; HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadFunction, this, 0, &threadID); if (hThread == 0) { printf("create thread fail, errno=%d, doserrno=%d\n", errno, _doserrno); return FALSE; } this->m_threadID = threadID; this->m_threadHandle = hThread; //printf("new thread [%d]\n", threadID); return TRUE; }
void CThread::SetThreadName(char* thrName) { if (NULL != m_threadName) { free(m_threadName); m_threadName = NULL; } if (NULL != thrName) { int mallocSize = strlen(thrName) + 1; m_threadName = (char*)malloc(mallocSize); memset(m_threadName, 0, mallocSize); strncpy_s(m_threadName, mallocSize, thrName, strlen(thrName)); } }
// WorkThread.h
#ifndef WORKER_THREAD_H_ #define WORKER_THREAD_H_ #include "Thread.h" #include "ThreadCondition.h" #include "ThreadMutex.h" class CJob; class CThreadPool; class CWorkerThread : public CThread { private: CThreadPool* m_threadPool; CJob* m_job; void* m_jobData; public: CThreadCondition m_jobCond; public: CWorkerThread(); virtual ~CWorkerThread(); void Run(); void Terminate(); void SetJob(CJob* job, void* jobData); CJob* GetJob(void) {return m_job;} void SetThreadPool(CThreadPool* thrPool); CThreadPool* GetT