C/C++——线程池:CThreadPoolManager

来源:互联网 发布:手机数据恢复精灵 编辑:程序博客网 时间:2024/06/06 07:37

时间:2016.11.05


线程池管理类:

////////////////////////////////////////////////////////              CThreadPoolManager.h//////////////////////////////////////////////////////#pragma onceclass CThreadPool;class CTask;class CThreadPoolManager{public:   CThreadPoolManager();   CThreadPoolManager( UINT nInitThreads );   ~CThreadPoolManager();public:   void AddTask( CTask* pTask );   void TerminateAll();private:   BOOL CreateThreadPool( UINT nInitThreads );   CThreadPool* m_pPool;   UINT m_nInitThreadInPool;};// 全局函数获取一个线程管理器CThreadManager* glbf_GetThreadManager();////////////////////////////////////////////////////////              CThreadPoolManager.cpp//////////////////////////////////////////////////////#include "stdafx.h"#include "CThreadPoolManager.h"#include "CThreadPool.h"// 默认全局对象CThreadPoolManager glbo_ThreadPoolManager(10);// 获得线程池的全局接口CThreadPoolManager * glbf_GetThreadPoolManager(){   return &glbo_ThreadPoolManager;}// 构造函数CThreadPoolManager::CThreadPoolManager (){     CreateThreadPool( 10 );   m_nInitThreadInPool = 10;}// 构造函数CThreadPoolManager::CThreadPoolManager ( UINT nInitThreads ){   CreateThreadPool( nInitThreads );   m_nInitThreadInPool = nInitThreads;}CThreadPoolManager::~CThreadPoolManager (){   if( m_pPool )   {       m_pPool->TerminateAll();       delete m_pPool;   }}BOOL CThreadPoolManager::CreateThreadPool( UINT nInitThreads ){   if( m_pPool == NULL )       m_pPool = new CThreadPool( nInitThreads );   if( m_pPool )       return TRUE;   return FALSE;}// 向线程池内添加任务void CThreadPoolManager::AddTask( CTask* pTask ){   if( m_pPool && pTask )       m_pPool->AddTask( pTask );}

【返回】

线程池:目录

0 0
原创粉丝点击