线程池 | Thread、ThreadPool类 1.线程池支持的模式 1 2 3 4 5 6 enum class PoolMode { MODE_FIXED, MODE_CACHED, };
2.Thread类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Thread { public : using ThreadFunc = std::function<void (int )>; Thread (ThreadFunc func); ~Thread (); void start () ; int getId () const ; private : ThreadFunc func_; static int generateId_; int threadId_; };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Thread::Thread (ThreadFunc func) :func_ (func), threadId_ (generateId_++) {} Thread::~Thread (){} void Thread::start () { std::thread t (func_,threadId_) ; t.detach (); } int Thread::getId () const { return threadId_; }
3.ThraedPool类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 class ThreadPool { public : ThreadPool (); ~ThreadPool (); void setMode (PoolMode mode) ; void setTaskQueMaxThresHold (int threshold) ; void setthreadSizeThresHold (int threshold) ; Result submitTask (std::shared_ptr<Task> sp) ; void start (int initThreadSize = std::thread::hardware_concurrency()) ; ThreadPool (const ThreadPool&) = delete ; ThreadPool& operator =(const ThreadPool&) = delete ; private : void threadFunc (int threadid) ; bool checkRunningState () const ; private : std::unordered_map<int , std::unique_ptr<Thread>> threads_; size_t initThreadSize_; std::atomic_int curThreadSize_; int threadSizeThresHold_; std::atomic_int idleThreadSize_; std::queue<std::shared_ptr<Task>> taskQue_; std::atomic_int taskSize_; int taskQueMaxThresHold_; std::mutex taskQueMtx_; std::condition_variable notFull_; std::condition_variable notEmpty_; std::condition_variable exitCoond_; PoolMode poolMode_; std::atomic_bool isPoolRunning_; };
0.常量、全局变量定义 1 2 3 4 5 6 const int TASK_MAX_THRESHHOLD = 1024 ;const int THREAD_MAX_THRESHHOLD = 10 ;const int THREAD_MAX_TDLE_TIME = 10 ;int Thread::generateId_ = 0 ;
数量少是因为测试方便,大家可以自行修改
###1.checkRunningState()
1 2 3 4 bool ThreadPool::checkRunningState () const { return isPoolRunning_; }
2.ThreadPool() 1 2 3 4 5 6 7 8 9 10 11 ThreadPool::ThreadPool () : initThreadSize_ (0 ), taskSize_ (0 ), idleThreadSize_ (0 ), threadSizeThresHold_ (THREAD_MAX_THRESHHOLD), taskQueMaxThresHold_ (TASK_MAX_THRESHHOLD), curThreadSize_ (0 ), poolMode_ (PoolMode::MODE_FIXED), isPoolRunning_ (false ) {}
3.setMode() 1 2 3 4 5 6 7 void ThreadPool::setMode (PoolMode mode) { if (checkRunningState ()) return ; poolMode_ = mode; }
4.setTaskQueMaxThresHold() 1 2 3 4 5 void ThreadPool::setTaskQueMaxThresHold (int threshold) { taskQueMaxThresHold_ = threshold; }
5.setthreadSizeThresHold() 1 2 3 4 5 6 7 8 9 10 void ThreadPool::setthreadSizeThresHold (int threshold) { if (checkRunningState ()) return ; if (poolMode_ == PoolMode::MODE_CACHED) { threadSizeThresHold_ = threshold; } }
6.start() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 void ThreadPool::start (int initThreadSize) { isPoolRunning_ = true ; initThreadSize_ = initThreadSize; curThreadSize_ = initThreadSize; for (int i = 0 ; i < initThreadSize_; i++) { auto ptr = std::make_unique <Thread>(std::bind (&ThreadPool::threadFunc, this ,std::placeholders::_1)); int threadId = ptr->getId (); threads_.emplace (threadId, std::move (ptr)); } for (int i = 0 ; i < initThreadSize_; i++) { threads_[i]->start (); idleThreadSize_++; } }
7.submitTask()、threadFunc() 线程池 | ThreadPool的submitTask和threadFunc方法-CSDN博客
8.~ThreadPool() 1 2 3 4 5 6 7 8 9 10 11 12 ThreadPool::~ThreadPool () { isPoolRunning_ = false ; std::unique_lock<std::mutex> lock (taskQueMtx_) ; notEmpty_.notify_all (); exitCoond_.wait (lock, [&]()->bool {return threads_.size () == 0 ; }); }
4.使用线程池的例子