class _ThreadPool
{
class _Job
{
public:
/// 任务处理
virtual void doJob();
};
class _Thread
{
public:
private:
_ThreadPool* _pool;
_Job* _job;
public:
static void* _threadFunc(void* param)
{
_Thread* thr = (_Thread*)param;
if (thr)
thr->doWork();
return 0;
}
void doWork()
{
while (...)
{
///把自己扔进_pool的空闲队列
/// 等待job信号
_job->doJob();
}
}
};
public:
/// 初始化线程池
InitializePool( ... )
/// 销毁线程池
DestroyPool(... )
public:
void doJob(_Job*)
{
/// 从空闲队列中取出Thread对象
/// 把job扔给thread
}
};
main
{
_ThreadPool pool;
pool.InitializePool();
//用户工作
创建_Job
pool.doJob(...)
pool.DestroyPool();
}
通用型线程池框架 C++
最新推荐文章于 2024-07-01 20:36:40 发布