這個代碼還沒有調試過, 不過應該問題不大, 下次用起來就直接用,調一下就好
#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__
#include <pthread.h>
#include <queue>
class CThreadJob;
class CWorkerThread {
public:
CWorkerThread();
virtual bool Start();
void ThreadLock();
void ThreadUnlock();
bool JobQueueEmpty();
void QueuePushJob(CThreadJob *job);
void ThreadCondWait();
CThreadJob *QueuePopJob();
bool IsExit();
virtual ~CWorkerThread();
private:
pthread_t m_Thread;
pthread_mutex_t m_Locker;
pthread_cond_t m_Cond;
int m_Tid;
std::queue<CThreadJob *> m_JobQueue;
bool m_Exit;
private:
static void *Hook(void *arg);
void ClearQueueJob();
};
class CThreadPool
{
public:
CThreadPool();
CThreadPool(int thread_num);
~CThreadPool();
bool SetThreadCount(int threadcount);
bool Start();
void PushJob(CThreadJob *job);
void TerminalAll();
private:
int m_ThreadNum;
CWorkerThread *m_ThreadList;
bool m_Stop;
int m_ThreadSerial;
};
#endif // !__THREAD_POOL_H__
#include "thread_pool.h"
#include "job.h"
CThreadPool::CThreadPool():
m_ThreadNum(10),
m_ThreadList(NULL),
m_Stop(false),
m_ThreadSerial(0)
{
}
CThreadPool::CThreadPool(int thread_num):
m_ThreadNum(thread_num),
m_ThreadList(NULL),
m_Stop(false),
m_ThreadSerial(0)
{
}
CThreadPool::~CThreadPool()
{
if (NULL != m_ThreadList)
{
delete[] m_ThreadList;
m_ThreadList = NULL;
}
}
bool CThreadPool::SetThreadCount(int threadcount)
{
if (NULL != m_ThreadList)
{
return false;
}
m_ThreadNum = threadcount;
return true;
}
bool CThreadPool::Start()
{
m_ThreadList = new CWorkerThread[m_ThreadNum];
if(m_ThreadList == NULL)
return false;
for (int i = 0; i < m_ThreadNum; i++)
{
if (!m_ThreadList->Start())
return false;
}
return true;
}
void CThreadPool::PushJob(CThreadJob * job)
{
m_ThreadSerial++;
if (m_ThreadNum == m_ThreadSerial) {
m_ThreadSerial = 0;
}
m_ThreadList[m_ThreadSerial].QueuePushJob(job);
}
void CThreadPool::TerminalAll()
{
if (NULL != m_ThreadList) {
delete[] m_ThreadList;
m_ThreadList = NULL;
}
}
CWorkerThread::CWorkerThread():
m_Exit(false)
{
ClearQueueJob();
}
bool CWorkerThread::Start()
{
if (0 != pthread_create(&m_Thread, NULL, Hook, static_cast<void *>(this)))
{
return false;
}
pthread_mutex_init(&m_Locker,NULL);
pthread_cond_init(&m_Cond, NULL);
return true;
}
void CWorkerThread::ThreadLock()
{
pthread_mutex_lock(&m_Locker);
}
void CWorkerThread::ThreadUnlock()
{
pthread_mutex_unlock(&m_Locker);
}
bool CWorkerThread::JobQueueEmpty()
{
return m_JobQueue.empty();
}
void CWorkerThread::QueuePushJob(CThreadJob * job)
{
ThreadLock();
m_JobQueue.push(job);
pthread_cond_signal(&m_Cond);
ThreadUnlock();
}
void CWorkerThread::ThreadCondWait()
{
pthread_cond_wait(&m_Cond , &m_Locker);
}
CThreadJob * CWorkerThread::QueuePopJob()
{
CThreadJob *job = m_JobQueue.front();
m_JobQueue.pop();
return job;
}
bool CWorkerThread::IsExit()
{
return m_Exit;
}
CWorkerThread::~CWorkerThread()
{
m_Exit = true;
pthread_cond_signal(&m_Cond);
pthread_join(m_Thread, NULL);
ClearQueueJob();
}
void * CWorkerThread::Hook(void * arg)
{
CWorkerThread *thread = static_cast<CWorkerThread *> (arg);
thread->ThreadLock();
while (!thread->IsExit())
{
if (thread->JobQueueEmpty()) {
thread->ThreadCondWait();
continue;
}
CThreadJob *job = thread->QueuePopJob();
thread->ThreadUnlock();
if (NULL != job) {
job->Process(NULL);
delete job;
}
thread->ThreadLock();
}
return NULL;
}
void CWorkerThread::ClearQueueJob()
{
while (!m_JobQueue.empty()) {
CThreadJob *job = QueuePopJob();
if (NULL != job) {
delete job;
}
}
}
#ifndef __THREAD_JOB_H__
#define __THREAD_JOB_H__
class CThreadJob {
public:
virtual void Process(void *args) =0;
virtual ~CThreadJob();
};
#endif