ToB企服应用市场:ToB评测及商务社交产业平台

标题: [项目][WebServer][ThreadPool]详细讲解 [打印本页]

作者: 王柳    时间: 2024-9-29 02:19
标题: [项目][WebServer][ThreadPool]详细讲解
  1. static const int THREAD_POOL_NUM = 10;
  2. // 单例模式
  3. class ThreadPool
  4. {
  5. public:
  6.     static ThreadPool *GetInstance(int num = THREAD_POOL_NUM)
  7.     {
  8.         static pthread_mutex_t sMtx = PTHREAD_MUTEX_INITIALIZER;
  9.         if (_tp == nullptr)
  10.         {
  11.             pthread_mutex_lock(&sMtx);
  12.             if (_tp == nullptr) // 双重判断,以防线程安全问题
  13.             {
  14.                 _tp = new ThreadPool(num);
  15.                 _tp->Init();
  16.             }
  17.             pthread_mutex_unlock(&sMtx);
  18.         }
  19.         return _tp;
  20.     }
  21.     // static使该成员函数没有this指针,因为线程执行的函数只能有一个void*参数
  22.     static void *ThreadRoutine(void *args)
  23.     {
  24.         ThreadPool *tp = (ThreadPool *)args;
  25.         while(true)
  26.         {
  27.             Task task;
  28.             tp->Lock();
  29.             while(tp->TaskQueueIsEmpty()) // while防止伪唤醒
  30.             {
  31.                 tp->ThreadWait();
  32.             }
  33.             tp->Pop(task);
  34.             tp->Unlock(); // 注意,不要在临界资源区内处理任务哦~
  35.             task.ProcessOn();
  36.         }
  37.     }
  38.     bool Init()
  39.     {
  40.         for (int i = 0; i < _num; i++)
  41.         {
  42.             pthread_t tid;
  43.             if (pthread_create(&tid, nullptr, ThreadRoutine, this) != 0)
  44.             {
  45.                 LOG(FATAL, "Create ThreadPool Error");
  46.                 return false;
  47.             }
  48.         }
  49.         LOG(INFO, "Create ThreadPool Success");
  50.         
  51.         return true;
  52.     }
  53.     void Push(const Task& task) // in
  54.     {
  55.         Lock();
  56.         _taskQueue.push(task); // 任务队列为临界资源,操作要加锁
  57.         Unlock();
  58.         ThreadWakeUp();
  59.     }
  60.     void Pop(Task& task) // out
  61.     {
  62.         task = _taskQueue.front();
  63.         _taskQueue.pop();
  64.     }
  65.     void ThreadWait()
  66.     {
  67.         pthread_cond_wait(&_cond, &_mtx);
  68.     }
  69.     void ThreadWakeUp()
  70.     {
  71.         pthread_cond_signal(&_cond);
  72.     }
  73.     bool TaskQueueIsEmpty()
  74.     {
  75.         return !_taskQueue.size();
  76.     }
  77.     void Lock()
  78.     {
  79.         pthread_mutex_lock(&_mtx);
  80.     }
  81.     void Unlock()
  82.     {
  83.         pthread_mutex_unlock(&_mtx);
  84.     }
  85.     bool IsStop()
  86.     {
  87.         return _stop;
  88.     }
  89.     ~ThreadPool()
  90.     {
  91.         pthread_mutex_destroy(&_mtx);
  92.         pthread_cond_destroy(&_cond);
  93.     }
  94. private:
  95.     ThreadPool(int num = THREAD_POOL_NUM)
  96.         : _num(num), _stop(false)
  97.     {
  98.         pthread_mutex_init(&_mtx, nullptr);
  99.         pthread_cond_init(&_cond, nullptr);
  100.     }
  101.     ThreadPool(const ThreadPool &) = delete;
  102. private:
  103.     int _num;
  104.     bool _stop;
  105.     std::queue<Task> _taskQueue;
  106.     pthread_mutex_t _mtx;
  107.     pthread_cond_t _cond;
  108.     static ThreadPool *_tp;
  109. };
  110. ThreadPool* ThreadPool::_tp = nullptr;
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4