Linux环境下C++实现线程池
发布日期:2021-05-07 22:56:08 浏览次数:21 分类:精选文章

本文共 3078 字,大约阅读时间需要 10 分钟。

什么是线程池?

线程池是一种高效的线程使用模式,通过维护一组线程来管理任务的执行。其核心思想是避免在处理短时间任务时频繁创建和销毁线程,从而减少调度开销,提升系统性能。

线程池的工作原理

线程池通过维护一个可控的线程池大小,确保系统并发执行的任务数量与硬件资源(如CPU、内存)相匹配。关键特点包括:

  • 任务处理机制:线程池维护一个任务队列,线程从队列中获取任务并执行。
  • 线程管理:线程池创建固定或可变数量的线程,等待任务到来后自动启动。
  • 资源调度:线程池通过锁和条件变量实现线程间的任务分配和同步,确保任务高效执行。
  • 线程池的应用场景

    线程池适用于需要大量线程并发处理的任务场景,具体表现为:

  • 短任务处理:如HTTP服务器处理网页请求,线程池能高效处理大量短任务。
  • 资源受限环境:线程池可根据系统资源(如CPU核数)调整线程数量,避免资源浪费。
  • 任务负载波动:线程池能自动调节线程数量,应对任务波动,减少系统性能波动。
  • 线程池实现

    在Linux环境下,使用C++语言实现线程池需要以下关键步骤:

  • 任务类设计:定义任务类,包含任务数据和执行逻辑。
  • 线程池类设计:包括线程池的容量、退出标志、线程数量等成员变量,以及相关操作函数。
  • 线程创建与管理:通过pthread_create创建线程,线程执行轮询任务并从队列中获取任务。
  • 任务队列管理:线程池维护任务队列,主线程和工作线程通过队列进行任务交换。
  • 线程池示例

    以下是线程池的实现代码示例:

    #include 
    #include
    #include
    #include
    #include
    #define NUM 10
    class Task {
    public:
    Task(int data) : _data(data) {}
    void run() {
    std::cout << "i am " << pthread_self() << " i get a data: " << _data << std::endl;
    }
    ~Task() {}
    private:
    int _data;
    };
    class ThreadPool {
    public:
    ThreadPool(size_t cap = NUM) : _cap(cap), _quit(false), _threadNums(0) {
    pthread_mutex_init(&_lock, nullptr);
    pthread_cond_init(&_cond, nullptr);
    _threadNums = cap;
    }
    ~ThreadPool() {
    pthread_mutex_destroy(&_lock);
    pthread_cond_destroy(&_cond);
    }
    bool IsEmpty() {
    return _q.empty();
    }
    static void* Rountine(void* arg) {
    pthread_detach(pthread_self());
    ThreadPool* mythis = (ThreadPool*)arg;
    while (!mythis->_quit) {
    if (mythis->_q.empty()) {
    pthread_cond_wait(&_cond, &_lock);
    continue;
    }
    Task* data_p = mythis->_q.front();
    mythis->_q.pop();
    data_p->run();
    }
    mythis->_threadNums--;
    pthread_mutex_unlock(&_lock);
    std::cout << "i am " << pthread_self() << " i am done..." << std::endl;
    return NULL;
    }
    void push(Task& t) {
    pthread_mutex_lock(&_lock);
    _q.push(&t);
    if (_q.size() == _cap) {
    std::cout << "the queue is full you should increase the number of threads" << std::endl;
    }
    pthread_mutex_unlock(&_lock);
    pthread_cond_signal(&_cond);
    }
    void quit() {
    pthread_mutex_lock(&_lock);
    _quit = true;
    pthread_mutex_unlock(&_lock);
    while (_threadNums > 0) {
    pthread_cond_broadcast(&_cond);
    }
    }
    void creat() {
    for (int i = 0; i < _threadNums; ++i) {
    pthread_t tid;
    pthread_create(&tid, nullptr, Rountine, this);
    }
    }
    private:
    std::queue
    _q; size_t _cap; bool _quit; int _threadNums; pthread_mutex_t _lock; pthread_cond_t _cond; };
    #include "ThreadPool.hpp"
    int main() {
    ThreadPool pool;
    int i = 0;
    while (i++ < 10) {
    int data = rand() % 100;
    Task t(data);
    pool.push(t);
    sleep(1);
    }
    pool.quit();
    std::cout << "the ThreadPool is over" << std::endl;
    return 0;
    }

    运行结果

    运行上述代码,您将看到多个线程执行任务的输出,表明线程池成功实现了任务的并发处理。

    上一篇:详解C++继承(普通继承,菱形继承与虚拟继承)
    下一篇:多线程操作:互斥、同步与信号量(生产者消费者模型两种实现方式)

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月22日 06时14分32秒