未完成待完成代码:
#include<iostream> #include<pthread.h> #include<cstdio> #include<list> #include<exception> using namespace std; #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/lock_types.hpp> #include <boost/thread/condition_variable.hpp> using namespace boost; typedef boost::mutex CMutex; typedef boost::condition_variable_any CCondVar; typedef boost::lock_guard<CMutex> CLockGuard; typedef boost::unique_lock<CMutex> CUniqueLock; typedef boost::thread_group CThreadGroup; class CSemaphore{ private: int iResCnt; CMutex oMutex; CCondVar oCondVar; public: explicit CSemaphore(unsigned int res = 0) : iResCnt(res){} void Release(){ { CLockGuard oLockGuard(oMutex); iResCnt = iResCnt + 1; } oCondVar.notify_one(); } void WaitFor(){ CUniqueLock oUniqueLock(oMutex); while(iResCnt == 0){ oCondVar.wait(oUniqueLock); } iResCnt = iResCnt - 1; } }; class CJob{ private: int iTaskId; public: }; class CJobQueue{ private: bool bExit; std::list<Job> lstJob; CMutex oMutexLst; CCondition oCondJobInc; public: CJobQueue(){ bExit = false;} ~CJobQueue(){} void PutIntoQueue(const CJob& job){ CScopedLock oScopedLock(oMutexLst); lstJob.push_back(job); oCondJobInc.notify_all(); } void NotifyAll(){ bExit = true;//通知所有线程退出 oCondJobInc.notify_all();//唤醒所有线程 } CJob GetFromQueue(){ CScopedLock oScopedLock(oMutexLst); while(lstJob.size() == 0 && !bExit){ oCondJobInc.wait(oScopedLock); } CJob oJob; if(bExit) return oJob; oJob = lstJob.front(); lstJob.pop_front(); return oJob; } }; class CThreadPool{ private: int iThreads;//线程的数量 int iRequests;//请求队列的最大容量 CThreadGroup oThreadGroup;//线程组 //TaskQueue;//请求队列 CMutex oMutexQueue;//保护请求队列的互斥锁 CSemaphore oSemaphoreTask;//请求队列中是否有任务要处理 bool bStop;//是否结束线程 public: CThreadPool(int threads = 8, int requests = 100) : iThreas(threads), iRequests(requests){ if((iThreads <= 0) || (iRequests <= 0)) throw std::exception(); for(int i = 0; i < threads; i++){ oThreadGroup.create_thread(boost::bind(&CThreadGroupEx::Run, this, i)); } } ~CThreadPool(){ oThreadGroup.join_all(); } }; template<typename T> class threadpool { threadpool(int thread_number=8,int max_requests=1000); ~threadpool(); bool append(T* request); //向请求队列中添加请求 private: static void* worker(void *arg); //线程执行函数 void run(); //实际运行线程的函数 }; template<typename T> threadpool<T>::threadpool(int thread_number,int max_requests) :m_thread_number(thread_number) ,m_max_requests(max_requests) ,m_stop(false) ,m_threads(NULL) { if((thread_number<=0)||(max_requests<=0)) { throw std::exception(); } m_threads=new pthread_t[thread_number]; if(!m_threads) { throw std::exception(); } for(int i=0;i<thread_number;i++) //创建thread_number个线程,并且将其设置为分离状态 { if(pthread_create(m_threads+i,NULL,worker,(void*)this)!=0) { delete [] m_threads; throw std::exception(); } if(pthread_detach(m_threads[i])) { delete [] m_threads; throw std::exception(); } } } template<typename T> threadpool<T>::~threadpool() { delete [] m_threads; m_stop=true; } template<typename T> bool threadpool<T>::append(T* request) //向请求队列中添加请求任务 { m_queuelocker.lock(); if(m_workqueue.size()>m_max_requests) //确保请求队列中没有被任务堆积满 { m_queuelocker.unlock(); return false; } m_workqueue.push_back(request); m_queuelocker.unlock(); m_queuestat.post(); //没添加一个任务,信号量增加 return true; } template<typename T> void* threadpool<T>::worker(void *arg) { threadpool *pool=(threadpool*)arg; pool->run(); //调用run函数处理请求队列中的请求任务 return pool; } template<typename T> void threadpool<T>::run() //处理请求队列中的请求任务 { while(!m_stop) { m_queuestat.wait(); m_queuelocker.lock(); if(m_workqueue.empty()) { m_queuelocker.unlock(); continue; } T* request=m_workqueue.front(); m_workqueue.pop_front(); m_queuelocker.unlock(); request->process(); //process是任务类里面的一个方法 } }