C++实现线程池案例

c++ c++ 2396 人阅读 | 0 人回复

发表于 2022-11-20 17:23:07 | 显示全部楼层 |阅读模式

1.基础概念
线程池(thread pool): 一种线程使用模式,线程过多会带来调度开销,进而影响缓存局部性和整体性能。而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务。这避免了在短时间任务创建与销毁线程的代价。线程池不仅能够保证内核的充分利用,还能防止过分调度。可用线程数据取决于可用的并发处理器、处理器内核、内存、网络sockets等数量。
2. 线程池的组成2.1 线程池管理器
创建一定数量的线程,启动线程,调配任务,管理着线程池。
线程池目前只需要启动(start()),停止方法(stop()),及任务添加方法(addTask).
start()创建一定数量的线程池,进入线程循环.
stop()停止所有的线程循环,回收所有资源.
addTask()添加任务.
2.2 工作线程
线程池中线程,在线程池中等待并执行分配任务.
该文选用条件变量实现等待和通知机制
2.3 任务接口
添加任务接口,以供工作线程调度任务的执行.
2.4 任务队列
用于存放没有处理的任务,提供一种缓冲机制,同时任务队列具有调度功能,高级优先的任务放在任务队列前面;本文选用priority_queue与pair的结合用作任务优先队列结构.
3. 线程池工作的四种情况
假设我们的线程池大小为3,任务队列目前不做大小限制。
3.1 主程序当前没有任务要执行,线程池中任务队列为空闲状态
下面情况下所有工作线程处于空闲的等待状态,任务缓冲队列为空.
thread pool1.png

3.2 主程序添加小于等于线程池中线程数量得任务
基于3.1情况,所有的工作线程已处于等待状态,主线程开始添加三个任务,添加后通知(notif())唤醒线程池中的线程开始取(take())任务执行。此时的任务缓冲队列还是空。
thread pool2.png

3.3 主程序添加任务数量大于当前线程池中线程数量的任务
基于3.2情况,所有工作线程都在工作中,主线程开始添加第四个任务,添加后发现现在线程池中线程用完了,于是存入任务缓冲队列。工作线程空闲后主动从任务队列取任务执行。
thread pool3.png

3.4 主程序添加任务数量大于当前线程池中线程数量的任务,且任务缓冲队列已满
此情况发生情形3且设置了任务缓冲队列大小后面,主程序添加第N个任务,添加后发现线程池中线程已经用完了,任务缓冲队列已满,于是进入等待状态,等待任务缓冲队列中任务腾空通知。但是这种情形会阻塞主线程,本文不限制任务队列的大小,必要时再优化。
thread pool4.png

4. 线程池的C++实现
thread pool5.png

线程池的主要组成由三个部分构成:
  • 任务队列(Task Quene)
  • 线程池(Thread Pool)
  • 完成队列(Completed Tasks)
等待通知机制通过条件变量实现,Logger和CurrentThread,用于调试,可以无视。
  1. #ifndef _THREADPOOL_HH
  2. #define _THREADPOOL_HH

  3. #include <vector>
  4. #include <utility>
  5. #include <queue>
  6. #include <thread>
  7. #include <functional>
  8. #include <mutex>

  9. #include "Condition.hh"

  10. class ThreadPool{
  11. public:
  12.   static const int kInitThreadsSize = 3;
  13.   enum taskPriorityE { level0, level1, level2, };
  14.   typedef std::function<void()> Task;
  15.   typedef std::pair<taskPriorityE, Task> TaskPair;

  16.   ThreadPool();
  17.   ~ThreadPool();

  18.   void start();
  19.   void stop();
  20.   void addTask(const Task&);
  21.   void addTask(const TaskPair&);

  22. private:
  23.   ThreadPool(const ThreadPool&);//禁止复制拷贝.
  24.   const ThreadPool& operator=(const ThreadPool&);

  25.   struct TaskPriorityCmp
  26.   {
  27.     bool operator()(const ThreadPool::TaskPair p1, const ThreadPool::TaskPair p2)
  28.     {
  29.         return p1.first > p2.first; //first的小值优先
  30.     }
  31.   };

  32.   void threadLoop();
  33.   Task take();

  34.   typedef std::vector<std::thread*> Threads;
  35.   typedef std::priority_queue<TaskPair, std::vector<TaskPair>, TaskPriorityCmp> Tasks;

  36.   Threads m_threads;
  37.   Tasks m_tasks;

  38.   std::mutex m_mutex;
  39.   Condition m_cond;
  40.   bool m_isStarted;
  41. };

  42. #endif

  43. //Cpp

  44. #include <assert.h>

  45. #include "Logger.hh" // debug
  46. #include "CurrentThread.hh" // debug
  47. #include "ThreadPool.hh"

  48. ThreadPool::ThreadPool()
  49.   :m_mutex(),
  50.   m_cond(m_mutex),
  51.   m_isStarted(false)
  52. {

  53. }

  54. ThreadPool::~ThreadPool()
  55. {
  56.   if(m_isStarted)
  57.   {
  58.     stop();
  59.   }
  60. }

  61. void ThreadPool::start()
  62. {
  63.   assert(m_threads.empty());
  64.   m_isStarted = true;
  65.   m_threads.reserve(kInitThreadsSize);
  66.   for (int i = 0; i < kInitThreadsSize; ++i)
  67.   {
  68.     m_threads.push_back(new std::thread(std::bind(&ThreadPool::threadLoop, this)));
  69.   }

  70. }

  71. void ThreadPool::stop()
  72. {
  73.   LOG_TRACE << "ThreadPool::stop() stop.";
  74.   {
  75.     std::unique_lock<std::mutex> lock(m_mutex);
  76.     m_isStarted = false;
  77.     m_cond.notifyAll();
  78.     LOG_TRACE << "ThreadPool::stop() notifyAll().";
  79.   }

  80.   for (Threads::iterator it = m_threads.begin(); it != m_threads.end() ; ++it)
  81.   {
  82.     (*it)->join();
  83.     delete *it;
  84.   }
  85.   m_threads.clear();
  86. }


  87. void ThreadPool::threadLoop()
  88. {
  89.   LOG_TRACE << "ThreadPool::threadLoop() tid : " << CurrentThread::tid() << " start.";
  90.   while(m_isStarted)
  91.   {
  92.     Task task = take();
  93.     if(task)
  94.     {
  95.       task();
  96.     }
  97.   }
  98.   LOG_TRACE << "ThreadPool::threadLoop() tid : " << CurrentThread::tid() << " exit.";
  99. }

  100. void ThreadPool::addTask(const Task& task)
  101. {
  102.   std::unique_lock<std::mutex> lock(m_mutex);
  103.   /*while(m_tasks.isFull())
  104.     {//when m_tasks have maxsize
  105.       cond2.wait();
  106.     }
  107.   */
  108.   TaskPair taskPair(level2, task);
  109.   m_tasks.push(taskPair);
  110.   m_cond.notify();
  111. }

  112. void ThreadPool::addTask(const TaskPair& taskPair)
  113. {
  114.   std::unique_lock<std::mutex> lock(m_mutex);
  115.   /*while(m_tasks.isFull())
  116.     {//when m_tasks have maxsize
  117.       cond2.wait();
  118.     }
  119.   */
  120.   m_tasks.push(taskPair);
  121.   m_cond.notify();
  122. }

  123. ThreadPool::Task ThreadPool::take()
  124. {
  125.   std::unique_lock<std::mutex> lock(m_mutex);
  126.   //always use a while-loop, due to spurious wakeup
  127.   while(m_tasks.empty() && m_isStarted)
  128.   {
  129.     LOG_TRACE << "ThreadPool::take() tid : " << CurrentThread::tid() << " wait.";
  130.     m_cond.wait(lock);
  131.   }

  132.   LOG_TRACE << "ThreadPool::take() tid : " << CurrentThread::tid() << " wakeup.";

  133.   Task task;
  134.   Tasks::size_type size = m_tasks.size();
  135.   if(!m_tasks.empty() && m_isStarted)
  136.   {
  137.     task = m_tasks.top().second;
  138.     m_tasks.pop();
  139.     assert(size - 1 == m_tasks.size());
  140.     /*if (TaskQueueSize_ > 0)
  141.     {
  142.       cond2.notify();
  143.     }*/
  144.   }

  145.   return task;

  146. }
复制代码

4.1 队列
队列作为先进先出的数据结构,当有可用的工作时,线程从队列中获取工作并执行。如果两个线程同时执行相同的工作会出现程序崩溃。为了避免这种问题,需要再标准C++ Queue上实现一个包装器,使用mutex来限制并发访问。
void enqueue(T& t) {      
    std::unique_lock<std::mutex> lock(m_mutex);   
    m_queue.push(t);
}
要排队做的第一件事情就是锁定互斥锁来确保没有其他人正在访问该资源。然后,将元素推送到队列当中。当锁超出范围时,它会自动释放,这样使Queue线程安全,因此不用担心许多线程在相同时间访问或者修改它。
4.2 提交函数
线程池最重要的方法是负责向队列添加任务。
5. 测试程序5.1 start()、stop()
测试线程池基本的创建退出工作,以及检测资源是否回收正常。
  1. int main(){
  2.   {
  3.   ThreadPool threadPool;
  4.   threadPool.start();
  5.   getchar();}

  6.   getchar();
  7.   return 0;
  8. }
复制代码
  1. ./test.out
  2. 2018-11-25 16:50:36.054805 [TRACE] [ThreadPool.cpp:53] [threadLoop] ThreadPool::threadLoop() tid : 3680 start.
  3. 2018-11-25 16:50:36.054855 [TRACE] [ThreadPool.cpp:72] [take] ThreadPool::take() tid : 3680 wait.
  4. 2018-11-25 16:50:36.055633 [TRACE] [ThreadPool.cpp:53] [threadLoop] ThreadPool::threadLoop() tid : 3679 start.
  5. 2018-11-25 16:50:36.055676 [TRACE] [ThreadPool.cpp:72] [take] ThreadPool::take() tid : 3679 wait.
  6. 2018-11-25 16:50:36.055641 [TRACE] [ThreadPool.cpp:53] [threadLoop] ThreadPool::threadLoop() tid : 3681 start.
  7. 2018-11-25 16:50:36.055701 [TRACE] [ThreadPool.cpp:72] [take] ThreadPool::take() tid : 3681 wait.
  8. 2018-11-25 16:50:36.055736 [TRACE] [ThreadPool.cpp:53] [threadLoop] ThreadPool::threadLoop() tid : 3682 start.
  9. 2018-11-25 16:50:36.055746 [TRACE] [ThreadPool.cpp:72] [take] ThreadPool::take() tid : 3682 wait.

  10. 2018-11-25 16:51:01.411792 [TRACE] [ThreadPool.cpp:36] [stop] ThreadPool::stop() stop.
  11. 2018-11-25 16:51:01.411863 [TRACE] [ThreadPool.cpp:39] [stop] ThreadPool::stop() notifyAll().
  12. 2018-11-25 16:51:01.411877 [TRACE] [ThreadPool.cpp:76] [take] ThreadPool::take() tid : 3680 wakeup.
  13. 2018-11-25 16:51:01.411883 [TRACE] [ThreadPool.cpp:62] [threadLoop] ThreadPool::threadLoop() tid : 3680 exit.
  14. 2018-11-25 16:51:01.412062 [TRACE] [ThreadPool.cpp:76] [take] ThreadPool::take() tid : 3682 wakeup.
  15. 2018-11-25 16:51:01.412110 [TRACE] [ThreadPool.cpp:62] [threadLoop] ThreadPool::threadLoop() tid : 3682 exit.
  16. 2018-11-25 16:51:01.413052 [TRACE] [ThreadPool.cpp:76] [take] ThreadPool::take() tid : 3679 wakeup.
  17. 2018-11-25 16:51:01.413098 [TRACE] [ThreadPool.cpp:62] [threadLoop] ThreadPool::threadLoop() tid : 3679 exit.
  18. 2018-11-25 16:51:01.413112 [TRACE] [ThreadPool.cpp:76] [take] ThreadPool::take() tid : 3681 wakeup.
  19. 2018-11-25 16:51:01.413141 [TRACE] [ThreadPool.cpp:62] [threadLoop] ThreadPool::threadLoop() tid : 3681 exit.
复制代码

5.2 addTask()、 PriorityTaskQueue
测试添加任务接口,以及优先级任务队列。主线程首先添加5个普通任务,1s后添加一个高优先级任务,当前3个线程中的最先一个空闲后,会最先执行后面添加的priorityFunc().
  1. std::mutex g_mutex;

  2. void priorityFunc()
  3. {
  4.   for (int i = 1; i < 4; ++i)
  5.   {
  6.       std::this_thread::sleep_for(std::chrono::seconds(1));
  7.       std::lock_guard<std::mutex> lock(g_mutex);
  8.       LOG_DEBUG << "priorityFunc() [" << i << "at thread [ " << CurrentThread::tid() << "] output";// << std::endl;
  9.   }

  10. }

  11. void testFunc()
  12. {
  13.   // loop to print character after a random period of time
  14.   for (int i = 1; i < 4; ++i)
  15.   {
  16.       std::this_thread::sleep_for(std::chrono::seconds(1));
  17.       std::lock_guard<std::mutex> lock(g_mutex);
  18.       LOG_DEBUG << "testFunc() [" << i << "] at thread [ " << CurrentThread::tid() << "] output";// << std::endl;
  19.   }

  20. }


  21. int main()
  22. {
  23.   ThreadPool threadPool;
  24.   threadPool.start();

  25.   for(int i = 0; i < 5 ; i++)
  26.     threadPool.addTask(testFunc);

  27.   std::this_thread::sleep_for(std::chrono::seconds(1));

  28.   threadPool.addTask(ThreadPool::TaskPair(ThreadPool::level0, priorityFunc));

  29.   getchar();
  30.   return 0;
  31. }
复制代码
  1. ./test.out
  2. 2018-11-25 18:24:20.886837 [TRACE] [ThreadPool.cpp:56] [threadLoop] ThreadPool::threadLoop() tid : 4121 start.
  3. 2018-11-25 18:24:20.886893 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4121 wakeup.
  4. 2018-11-25 18:24:20.887580 [TRACE] [ThreadPool.cpp:56] [threadLoop] ThreadPool::threadLoop() tid : 4120 start.
  5. 2018-11-25 18:24:20.887606 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4120 wakeup.
  6. 2018-11-25 18:24:20.887610 [TRACE] [ThreadPool.cpp:56] [threadLoop] ThreadPool::threadLoop() tid : 4122 start.
  7. 2018-11-25 18:24:20.887620 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4122 wakeup.
  8. 2018-11-25 18:24:21.887779 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4120] output
  9. 2018-11-25 18:24:21.887813 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4122] output
  10. 2018-11-25 18:24:21.888909 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4121] output
  11. 2018-11-25 18:24:22.888049 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4120] output
  12. 2018-11-25 18:24:22.888288 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4122] output
  13. 2018-11-25 18:24:22.889978 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4121] output
  14. 2018-11-25 18:24:23.888467 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4120] output
  15. 2018-11-25 18:24:23.888724 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4120 wakeup.
  16. 2018-11-25 18:24:23.888778 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4122] output
  17. 2018-11-25 18:24:23.888806 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4122 wakeup.
  18. 2018-11-25 18:24:23.890413 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4121] output
  19. 2018-11-25 18:24:23.890437 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4121 wakeup.
  20. 2018-11-25 18:24:24.889247 [DEBUG] [main.cpp:92] [priorityFunc] priorityFunc() [1at thread [ 4120] output
  21. 2018-11-25 18:24:24.891187 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4121] output
  22. 2018-11-25 18:24:24.893163 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4122] output
  23. 2018-11-25 18:24:25.889567 [DEBUG] [main.cpp:92] [priorityFunc] priorityFunc() [2at thread [ 4120] output
  24. 2018-11-25 18:24:25.891477 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4121] output
  25. 2018-11-25 18:24:25.893450 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4122] output
  26. 2018-11-25 18:24:26.890295 [DEBUG] [main.cpp:92] [priorityFunc] priorityFunc() [3at thread [ 4120] output
  27. 2018-11-25 18:24:26.890335 [TRACE] [ThreadPool.cpp:99] [take] ThreadPool::take() tid : 4120 wait.
  28. 2018-11-25 18:24:26.892265 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4121] output
  29. 2018-11-25 18:24:26.892294 [TRACE] [ThreadPool.cpp:99] [take] ThreadPool::take() tid : 4121 wait.
  30. 2018-11-25 18:24:26.894274 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4122] output
  31. 2018-11-25 18:24:26.894299 [TRACE] [ThreadPool.cpp:99] [take] ThreadPool::take() tid : 4122 wait.

  32. 2018-11-25 18:24:35.359003 [TRACE] [ThreadPool.cpp:37] [stop] ThreadPool::stop() stop.
  33. 2018-11-25 18:24:35.359043 [TRACE] [ThreadPool.cpp:42] [stop] ThreadPool::stop() notifyAll().
  34. 2018-11-25 18:24:35.359061 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4120 wakeup.
  35. 2018-11-25 18:24:35.359067 [TRACE] [ThreadPool.cpp:65] [threadLoop] ThreadPool::threadLoop() tid : 4120 exit.
  36. 2018-11-25 18:24:35.359080 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4122 wakeup.
  37. 2018-11-25 18:24:35.359090 [TRACE] [ThreadPool.cpp:65] [threadLoop] ThreadPool::threadLoop() tid : 4122 exit.
  38. 2018-11-25 18:24:35.359123 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4121 wakeup.
  39. 2018-11-25 18:24:35.359130 [TRACE] [ThreadPool.cpp:65] [threadLoop] ThreadPool::threadLoop() tid : 4121 exit.
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则