📄 threadpool.cpp
字号:
//////////////////////////////////////////////////////////////////////// // ThreadPool.cpp // // by Reiot// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// include files//////////////////////////////////////////////////#include <algorithm>#include "ThreadPool.h"#include "Assert.h"#include "Thread.h"#include "LogClient.h"//////////////////////////////////////////////////// function object for find_if ()////////////////////////////////////////////////////// 牧抛捞呈加狼 静饭靛 按眉啊 漂沥 TID甫 爱绊 乐阑 版快 true甫 府畔茄促.//class isSameTID {public : // constructor isSameTID (TID tid) : m_TID(tid) {} // bool operator () (Thread* pThread) throw () { return pThread->getTID() == m_TID; }private : // thread identifier TID m_TID;};//////////////////////////////////////////////////////////////////////// constructor//////////////////////////////////////////////////////////////////////ThreadPool::ThreadPool () throw (){ __BEGIN_TRY m_Mutex.setName("ThreadPool"); __END_CATCH} //////////////////////////////////////////////////////////////////////// destructor// 器窃窍绊 乐绰 葛电 静饭靛 按眉甫 昏力秦具 茄促.//////////////////////////////////////////////////////////////////////ThreadPool::~ThreadPool () throw (){ __BEGIN_TRY ////////////////////////////////////////////////// // enter critical section ////////////////////////////////////////////////// m_Mutex.lock(); /* list<Thread*>::iterator itr = m_Threads.begin(); for (; itr != m_Threads.end() ; itr ++) { Thread* temp = *itr; // 静饭靛绰 辆丰茄 惑怕咯具 茄促. Assert(temp != NULL && temp->getStatus() == Thread::EXIT); SAFE_DELETE(temp); } m_Threads.erase(m_Threads.begin() , m_Threads.end()); g_pLogManager->Log5("after erase(begin , end) , list's size == %d\n" , m_Threads.size()); */ list<Thread*>::iterator itr; while ((itr = m_Threads.begin()) != m_Threads.end()) { // 酒流档 府胶飘俊 畴靛啊 巢酒乐促绰 舵捞促. // 静饭靛绰 辆丰茄 惑怕咯具 茄促. Assert(*itr != NULL && (*itr)->getStatus() == Thread::EXIT); SAFE_DELETE(*itr); m_Threads.pop_front(); } ////////////////////////////////////////////////// // leave critical section ////////////////////////////////////////////////// m_Mutex.unlock(); __END_CATCH}//////////////////////////////////////////////////////////////////////// 静饭靛钱救俊 殿废等 静饭靛甸阑 RUNNING 惑怕肺 父电促. //////////////////////////////////////////////////////////////////////void ThreadPool::start () throw (Error){ __BEGIN_TRY log(LOG_DEBUG_MSG, "", "", "== ThreadPool has started =="); ////////////////////////////////////////////////// // enter critical section ////////////////////////////////////////////////// m_Mutex.lock(); for (list<Thread*>::iterator itr = m_Threads.begin() ; itr != m_Threads.end() ; itr ++) { // start threads Assert(*itr != NULL); (*itr)->start(); string msg = "== " + (*itr)->getName() + " has been started == "; log(LOG_DEBUG_MSG, "", "", msg); } ////////////////////////////////////////////////// // leave critical section ////////////////////////////////////////////////// m_Mutex.unlock(); __END_CATCH} //////////////////////////////////////////////////////////////////////// 静饭靛钱救俊 殿废等 葛电 静饭靛狼 角青阑 吝窜矫挪促.// (捞绰 singal 趣篮 cancellation 栏肺 备泅秦具 窍摆促.)//////////////////////////////////////////////////////////////////////void ThreadPool::stop () throw (Error){ __BEGIN_TRY throw UnsupportedError("do not use this method now..."); __END_CATCH} //////////////////////////////////////////////////////////////////////// 静饭靛钱俊 静饭靛 按眉甫 殿废茄促.//////////////////////////////////////////////////////////////////////void ThreadPool::addThread (Thread* thread) throw (Error){ __BEGIN_TRY ////////////////////////////////////////////////// // enter critical section ////////////////////////////////////////////////// m_Mutex.lock(); // 静饭靛绰 澄捞 酒聪绢具 茄促. Assert(thread != NULL); // 府胶飘狼 盖 付瘤阜俊 静饭靛 按眉甫 火涝茄促. m_Threads.push_back(thread); string msg = "== " + thread->getName() + " added to thread pool"; log(LOG_DEBUG_MSG, "", "", msg); ////////////////////////////////////////////////// // leave critical section ////////////////////////////////////////////////// m_Mutex.unlock(); __END_CATCH} //////////////////////////////////////////////////////////////////////// 静饭靛钱俊辑 漂沥 静饭靛 按眉甫 昏力茄促.//////////////////////////////////////////////////////////////////////void ThreadPool::deleteThread (TID tid) throw (NoSuchElementException , Error){ __BEGIN_TRY ////////////////////////////////////////////////// // enter critical section ////////////////////////////////////////////////// m_Mutex.lock(); // function object肺 漂沥 TID甫 啊柳 静饭靛 按眉啊 淬变 畴靛甫 淬篮 // iterator甫 茫酒辰促. list<Thread*>::iterator itr = find_if (m_Threads.begin() , m_Threads.end() , isSameTID(tid)); if (itr != m_Threads.end()) // found! { // 静饭靛 按眉甫 烙矫肺 历厘秦敌促. Thread* temp = *itr; // 静饭靛绰 辆丰茄 惑怕咯具 茄促. // 窍困 努贰胶俊 Mutex啊 粮犁且 版快, getStatus(), setStatus()绰 Mutex肺 焊龋登绢具 茄促. Assert(temp != NULL && temp->getStatus() == Thread::EXIT); StringStream msg; msg << "== Thread[" << temp->getTID() << "] has been removed from ThreadPool =="; log(LOG_DEBUG_MSG, "", "", msg.toString()); // 静饭靛 按眉甫 昏力茄促. SAFE_DELETE(temp); // 畴靛甫 昏力茄促. m_Threads.erase(itr); } else // not found { StringStream buf; buf << "TID(" << tid << ")" ; ////////////////////////////////////////////////// // leave critical section ////////////////////////////////////////////////// m_Mutex.unlock(); throw NoSuchElementException(buf.toString()); } ////////////////////////////////////////////////// // leave critical section ////////////////////////////////////////////////// m_Mutex.unlock(); __END_CATCH} //////////////////////////////////////////////////////////////////////// 静饭靛钱俊辑 漂沥 静饭靛 按眉甫 茫酒辑 府畔茄促.//////////////////////////////////////////////////////////////////////Thread* ThreadPool::getThread (TID tid) throw (NoSuchElementException , Error){ __BEGIN_TRY Thread* thread = NULL; ////////////////////////////////////////////////// // enter critical section ////////////////////////////////////////////////// m_Mutex.lock(); list<Thread*>::iterator itr = find_if (m_Threads.begin() , m_Threads.end() , isSameTID(tid)); if (itr != m_Threads.end()) { // found Assert(*itr != NULL); thread = *itr; } else { // not found StringStream buf; buf << "TID(" << tid << ")" ; ////////////////////////////////////////////////// // leave critical section ////////////////////////////////////////////////// m_Mutex.unlock(); throw NoSuchElementException(buf.toString()); } ////////////////////////////////////////////////// // leave critical section ////////////////////////////////////////////////// m_Mutex.unlock(); return thread; __END_CATCH}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -