CThreadPool.h
#pragma once
#include <iostream>
#include <list>
#include <Windows.h>
#include <process.h>
using namespace std;
class Itask {
public:
Itask() {};
virtual ~Itask() {};
public:
virtual void RunItask() = 0;
};
class CThreadPool
{
public:
CThreadPool();
~CThreadPool();
public:
bool CreateThreadPool(long MinThreadNum, long MaxThreadNum); //创建线程池
void DestroyThreadPool(); //销毁线程池
static unsigned _stdcall ThreadProc(void *); //线程函数
bool Push(Itask *); //添加任务
private:
list<HANDLE>m_lstThread; //线程队列
list<Itask*>m_Itask; //任务队列
bool m_FlagQuit; //退出标志
long m_lCreThreadNum; //已创建线程数
long m_lRunThreadNum; //运行的线程数
long m_lMaxThreadNum; //最大线程数
HANDLE m_hSemaphore; //创建一个信号量
CRITICAL_SECTION m_cs; //创建一个关键段
};
CThreadPool.cpp
#include "CThreadPool.h"
CThreadPool::