基于C++11封装的线程池

为什么要用线程池

  • 降低资源消耗。通过重复利用已创建的线程降低线程创建、销毁线程造成的消耗。
  • 提高响应速度。当任务到达时,任务可以不需要等到线程创建就能立即执行。
  • 提高线程的可管理性。线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配、调优和监控
    下面是鄙人实现的一个线程池希望对你有所帮助!

ThreadPool.h

#pragma once

#include <functional>
#include <vector>
#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个基于C 11的线程池代码,包含线程池的创建、销毁、任务添加、任务执行等基本功能: ``` #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <stdbool.h> // 定义任务结构体 typedef struct task { void *(*func)(void *); // 任务函数 void *arg; // 任务参数 struct task *next; // 下一个任务 } task_t; // 定义线程池结构体 typedef struct thread_pool { pthread_mutex_t lock; // 互斥锁 pthread_cond_t notify; // 条件变量 pthread_t *threads; // 线程数组 task_t *queue; // 任务队列 bool is_shutdown; // 线程池是否关闭标志 int thread_count; // 线程数 int queue_size; // 任务队列长度 } thread_pool_t; // 初始化线程池 void thread_pool_init(thread_pool_t *pool, int thread_count, int queue_size) { pthread_mutex_init(&(pool->lock), NULL); pthread_cond_init(&(pool->notify), NULL); pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * thread_count); pool->queue = NULL; pool->is_shutdown = false; pool->thread_count = thread_count; pool->queue_size = queue_size; // 创建指定数量的线程 for (int i = 0; i < thread_count; i++) { pthread_create(&(pool->threads[i]), NULL, thread_pool_worker, (void *)pool); } } // 线程池工作函数 void *thread_pool_worker(void *arg) { thread_pool_t *pool = (thread_pool_t *)arg; while (true) { pthread_mutex_lock(&(pool->lock)); while (pool->queue == NULL && !pool->is_shutdown) { pthread_cond_wait(&(pool->notify), &(pool->lock)); } if (pool->is_shutdown) { pthread_mutex_unlock(&(pool->lock)); pthread_exit(NULL); } // 取出任务并执行 task_t *task = pool->queue; pool->queue = pool->queue->next; pthread_mutex_unlock(&(pool->lock)); (*(task->func))(task->arg); free(task); } } // 添加任务到线程池 void thread_pool_add_task(thread_pool_t *pool, void *(*func)(void *), void *arg) { task_t *task = (task_t *)malloc(sizeof(task_t)); task->func = func; task->arg = arg; task->next = NULL; pthread_mutex_lock(&(pool->lock)); if (pool->queue == NULL) { pool->queue = task; } else { task_t *temp = pool->queue; while (temp->next != NULL) { temp = temp->next; } temp->next = task; } pthread_cond_signal(&(pool->notify)); pthread_mutex_unlock(&(pool->lock)); } // 销毁线程池 void thread_pool_destroy(thread_pool_t *pool) { pool->is_shutdown = true; // 唤醒所有线程并等待它们退出 pthread_cond_broadcast(&(pool->notify)); for (int i = 0; i < pool->thread_count; i++) { pthread_join(pool->threads[i], NULL); } // 销毁任务队列 task_t *task; while (pool->queue != NULL) { task = pool->queue; pool->queue = task->next; free(task); } // 销毁互斥锁和条件变量 pthread_mutex_destroy(&(pool->lock)); pthread_cond_destroy(&(pool->notify)); free(pool->threads); } ``` 使用方法: 1. 首先定义一个 `thread_pool_t` 结构体变量; 2. 调用 `thread_pool_init` 函数初始化线程池,传入线程数和任务队列长度; 3. 调用 `thread_pool_add_task` 函数添加任务,传入任务函数和参数; 4. 线程池会自动执行添加的任务; 5. 最后调用 `thread_pool_destroy` 函数销毁线程池。 例如,下面的代码创建一个线程池,添加3个任务,然后销毁线程池: ``` void *task_func(void *arg) { int num = *((int *)arg); printf("Task %d is running.\n", num); usleep(100000); return NULL; } int main() { thread_pool_t pool; thread_pool_init(&pool, 2, 5); int args[3] = {1, 2, 3}; for (int i = 0; i < 3; i++) { thread_pool_add_task(&pool, task_func, (void *)&args[i]); } sleep(1); thread_pool_destroy(&pool); return 0; } ``` 输出结果为: ``` Task 1 is running. Task 2 is running. Task 3 is running. ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发如雪-ty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值