C++线程池怎么进行优化

wanwan_1996 2017-08-09 04:02:59
应该还有一些错误,希望大家帮我指哪里需要改进,优化。给出方案,谢谢大家了


#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
//#include <strstream>
#include <errno.h>
#include <algorithm>
using namespace std;

#define PTHREAD_OPT_ADD 1
#define PTHREAD_OPT_DEL -1
#define PTHREAD_MANAGER_SLEEP 1
#define MIN_WAIT_TASK_COUNTS 10
#define DEFAULT_ADD_THREAD_COUNTS 10
#define DEFAULT_DEL_THREAD_COUNTS DEFAULT_ADD_THREAD_COUNTS

class CTask
{
public:
CTask(){}
CTask(string name);
virtual ~CTask(){}

public:
virtual void run()
{
cout << "hello world" << endl;
}
void setdata(void *data);

protected:
string m_strTaskName;
void *m_ptrData;

};

class CThreadPool
{
public:
CThreadPool() {}
CThreadPool(int min_thrednum, int max_thrednum, int max_tasknum);
int AddTask(CTask *task);
int StopAll();
int getTaskSize();
bool ThreadAlive(pthread_t tid);

public:

static int Idle_ThreadNum;
static int Busy_ThreadNum;
static int Max_ThreadNum;
static int Min_ThreadNum;
static int Wait_ExitThreadNum;
static int Max_TaskCounts;

pthread_t ManagerThreadId;
static pthread_t *pthread_id;

static bool shutdown;
static vector<CTask *> m_vecTaskList;
static pthread_mutex_t m_pthreadMutex;
static pthread_cond_t m_pthreadCond;

protected:
int CreatePool();
static void *Thread_WorkerFunc(void *args);
static void *Thread_ManagerFunc(void *args);
};

#endif


#include "threadpool2.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//ESRCH No thread with the ID thread could be found.

vector<CTask *> CThreadPool::m_vecTaskList;
pthread_mutex_t CThreadPool::m_pthreadMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t CThreadPool::m_pthreadCond = PTHREAD_COND_INITIALIZER;
int CThreadPool::Min_ThreadNum;
int CThreadPool::Max_ThreadNum;
int CThreadPool::Max_TaskCounts;
int CThreadPool::Idle_ThreadNum;
int CThreadPool::Busy_ThreadNum;
int CThreadPool::Wait_ExitThreadNum;
bool CThreadPool::shutdown;
pthread_t * CThreadPool::pthread_id;

CThreadPool::CThreadPool(int min_thrednum, int max_thrednum, int max_tasknum)
{
Min_ThreadNum = min_thrednum;
Max_ThreadNum = max_thrednum;
Max_TaskCounts = max_tasknum;
Idle_ThreadNum = min_thrednum;
Busy_ThreadNum = 0;
Wait_ExitThreadNum = 0;
shutdown = false;

CreatePool();
}

void *CThreadPool::Thread_WorkerFunc(void *args)
{
CTask *stakTemp;

while(1)
{
pthread_mutex_lock(&m_pthreadMutex);

while((m_vecTaskList.size()) == 0 && (!shutdown))
{
cout << "等待的线程的id为:" << pthread_self() << endl;
pthread_cond_wait(&m_pthreadCond, &m_pthreadMutex);

if(Wait_ExitThreadNum > 0)
{
Wait_ExitThreadNum --;

if(Idle_ThreadNum > Min_ThreadNum)
{
Idle_ThreadNum --;
cout << "多余将要退出的线程id为:" << pthread_self() << endl;
pthread_mutex_unlock(&m_pthreadMutex);
pthread_exit(NULL);
}
}
}

if(shutdown)
{
pthread_mutex_unlock(&m_pthreadMutex);
cout << "退出的线程ID为:" << pthread_self() << endl;
pthread_exit(NULL);
}

vector<CTask *>::iterator iter = m_vecTaskList.begin();
if(iter != m_vecTaskList.end())
{
stakTemp = *iter;
m_vecTaskList.erase(iter);
}

Busy_ThreadNum ++;
pthread_mutex_unlock(&m_pthreadMutex);
stakTemp->run();

pthread_mutex_lock(&m_pthreadMutex);
Busy_ThreadNum --;
pthread_mutex_unlock(&m_pthreadMutex);
}

pthread_exit(NULL);
}

void *CThreadPool::Thread_ManagerFunc(void *args)
{
CThreadPool pool;
while(!shutdown)
{
cout << "我是管理线程" << endl;
// sleep(PTHREAD_MANAGER_SLEEP);
cout << "111111111111111111" << endl;
int tasksize = pool.getTaskSize();
pthread_mutex_lock(&m_pthreadMutex);
int alive = Idle_ThreadNum;
cout << "33333333333333333" << endl;
int busy = Busy_ThreadNum;
pthread_mutex_unlock(&m_pthreadMutex);
cout << "2222222222222222222222222222222222222222222222" << endl;
if(tasksize >= MIN_WAIT_TASK_COUNTS && Idle_ThreadNum < Max_ThreadNum)
{
cout << "添加" << endl;
pthread_mutex_lock(&m_pthreadMutex);

int add_threadnum = 0;
for(int i = 0; i < Max_ThreadNum && add_threadnum < DEFAULT_ADD_THREAD_COUNTS; i ++)
{
if(pthread_id[i] == 0 || !pool.ThreadAlive(pthread_id[i]))
{
pthread_create(&pthread_id[i], NULL, Thread_WorkerFunc, NULL);
add_threadnum ++;
Idle_ThreadNum ++;

cout << "将要新添加的新城的id为:" << pthread_id[i] << endl;
}
}

pthread_mutex_unlock(&m_pthreadMutex);
}

if(alive >= busy * 2 && alive > Min_ThreadNum)
{
cout << "删除" << endl;
pthread_mutex_lock(&m_pthreadMutex);
Wait_ExitThreadNum = DEFAULT_DEL_THREAD_COUNTS;
pthread_mutex_unlock(&m_pthreadMutex);
for(int i = 0; i < DEFAULT_DEL_THREAD_COUNTS; i ++)
{
pthread_cond_signal(&m_pthreadCond);
}
}

cout << "在工作" << endl;
}

pthread_exit(NULL);
}


int CThreadPool::CreatePool()
{
pthread_id = new pthread_t [Max_ThreadNum];
memset(pthread_id, 0, sizeof(pthread_id));

for(int i = 0; i < this->Idle_ThreadNum; ++ i)
{
pthread_create(&pthread_id[i], NULL, CThreadPool::Thread_WorkerFunc, NULL);
}

pthread_create(&ManagerThreadId, NULL, Thread_ManagerFunc, NULL);

return 0;
}
int CThreadPool::AddTask(CTask *task)
{
pthread_mutex_lock(&m_pthreadMutex);
m_vecTaskList.push_back(task);
cout << "add a task succe" << endl;
pthread_mutex_unlock(&m_pthreadMutex);
pthread_cond_signal(&m_pthreadCond);

return 0;
}
bool CThreadPool::ThreadAlive(pthread_t tid)
{
int kill_rc = pthread_kill(tid, 0);
if(kill_rc == ESRCH)
return false;

return true;
}
int CThreadPool::StopAll()
{
if(shutdown)
{
cout << "不可重复关闭" << endl;
return -1;
}

pthread_mutex_lock(&m_pthreadMutex);
shutdown = true;
pthread_cond_broadcast(&m_pthreadCond);
pthread_mutex_unlock(&m_pthreadMutex);

for(int i = 0; i < Idle_ThreadNum; ++i)
{
pthread_join(pthread_id[i], NULL);
}

delete []pthread_id;
pthread_id = NULL;
pthread_mutex_destroy(&m_pthreadMutex);
pthread_cond_destroy(&m_pthreadCond);

return 0;
}
int CThreadPool::getTaskSize()
{
pthread_mutex_lock(&m_pthreadMutex);
int counts = m_vecTaskList.size();
pthread_mutex_unlock(&m_pthreadMutex);

return counts;
}

int main (void)
{
cout << "begin............." << endl;
CThreadPool pool(3, 100, 100);


for(int i = 0; i < 200; ++ i)
{
CTask *task = new CTask();
pool.AddTask(task);
}
while(pool.getTaskSize() != 0)
{
cout << pool.getTaskSize() << endl;
continue;
}

pool.StopAll();
cout << "end..............." << endl;
return 0;
}



...全文
670 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

1,316

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧