C++ 线程池

线程池定义 (来自ChatGPT)

线程池是一种用于管理和复用线程的机制。它可以提高程序的性能和效率,特别是在处理大量并发任务时。

线程池中包含一定数量的线程,这些线程可以重复执行多个任务。当有任务需要执行时,可以将任务提交给线程池,线程池会选择一个可用的线程来执行任务。任务执行完毕后,线程会返回线程池,等待下一个任务的到来。

线程池的优点:

  1. 降低线程创建和销毁的开销:线程的创建和销毁是比较耗费资源的操作,使用线程池可以避免频繁地创建和销毁线程,提高程序的性能。
  2. 提高系统的响应速度:线程池中的线程可以立即执行任务,而不需要等待线程的创建和启动时间。
  3. 控制并发线程数:线程池可以限制同时执行的线程数量,避免系统资源被过度占用,提高系统的稳定性。
  4. 提供线程的管理和监控机制:线程池可以统一管理线程的状态、生命周期和执行情况,方便监控和调试。

线程池的缺点:

  1. 需要合理配置:线程池的性能和效果受到配置参数的影响,需要根据具体的应用场景和硬件环境来合理配置线程池的大小、任务队列的大小等参数。
  2. 可能引发资源泄露:如果线程池中的线程长时间闲置而不被使用,可能会导致资源的浪费和泄露。
  3. 可能引发死锁:在使用线程池时,如果任务之间存在依赖关系,可能会引发死锁问题,需要额外的注意和处理。

在使用线程池时,需要根据任务的类型和系统的负载情况来配置线程池的参数,如线程数量、任务队列的大小和拒绝策略等,以达到最佳的性能和资源利用率。

线程池实现

C++ 线程池实现

// ThreadPool.hpp

#ifndef _THREAD_POOL_HPP_
#define _THREAD_POOL_HPP_

#include <iostream>
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
#include <future>
#include <functional>
#include <condition_variable>

class ThreadPool {
 public:
  ThreadPool(size_t numThreads) {
    for (size_t i = 0; i < numThreads; ++i) {
      threads.emplace_back([this] {
        while (true) {
          std::function<void()> task;
          {
            std::unique_lock<std::mutex> lock(this->queue_mutex);
            this->condition.wait(
                lock, [this] { return this->stop || !this->tasks.empty(); });
            if (this->stop && this->tasks.empty()) {
              return;
            }
            task = std::move(this->tasks.front());
            this->tasks.pop();
          }
          task();
        }
      });
    }
  }

  template <typename Func, typename... Args>
  auto enqueue(Func&& func, Args&&... args)
      -> std::future<typename std::result_of<Func(Args...)>::type> {
    using return_type = typename std::result_of<Func(Args...)>::type;
    auto task = std::make_shared<std::packaged_task<return_type()>>(
        std::bind(std::forward<Func>(func), std::forward<Args>(args)...));
    std::future<return_type> res = task->get_future();
    {
      std::unique_lock<std::mutex> lock(queue_mutex);
      if (stop) {
        throw std::runtime_error("enqueue on stopped ThreadPool");
      }
      tasks.emplace([task]() { (*task)(); });
    }
    condition.notify_one();
    return res;
  }

  ~ThreadPool() {
    {
      std::unique_lock<std::mutex> lock(queue_mutex);
      stop = true;
    }
    condition.notify_all();
    for (std::thread& thread : threads) {
      thread.join();
    }
  }

 private:
  std::vector<std::thread> threads;
  std::queue<std::function<void()>> tasks;
  std::mutex queue_mutex;
  std::condition_variable condition;
  bool stop = false;
};

#endif

这个线程池实现使用了 C++11 的多线程库,包括 std::threadstd::mutexstd::condition_variable 等。它的主要功能是将任务添加到队列中,并在多个线程之间分配任务。具体来说,它包括以下几个部分:

  1. 构造函数:创建指定数量的线程,并将它们放入线程池中。
  2. enqueue 函数:将任务添加到队列中,并返回一个 std::future 对象,用于获取任务的返回值。
  3. 析构函数:停止所有线程,并等待它们完成任务。

enqueue 函数中,我们使用了 std::packaged_taskstd::bind 来将函数和参数绑定在一起,从而创建一个可调用对象。然后,我们将这个可调用对象封装在一个 std::function 对象中,并将它添加到任务队列中。最后,我们使用 std::condition_variable 来通知等待的线程有新的任务可用。

// main.cpp

#include <iostream>
#include <chrono>
#include "ThreadPool.hpp"

void task(int id) {
    std::cout << "Task " << id << " started" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Task " << id << " finished" << std::endl;
}

int main() {
    ThreadPool pool(4);

    for (int i = 0; i < 8; ++i) {
        pool.enqueue(task, i);
    }

    return 0;
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值