c++11线程池实现

本文介绍了C++11线程池的实现原理和语言细节。线程池通过管理任务队列和线程,实现任务的调度执行。文章详细讲解了如何使用C++11特性如函数对象、线程、互斥锁、条件变量等实现线程池,同时也讨论了C++11中的新特性在代码中的应用。
摘要由CSDN通过智能技术生成

咳咳。c++11 加入了线程库,从此告别了标准库不支持并发的历史。然而 c++ 对于多线程的支持还是比较低级,稍微高级一点的用法都需要自己去实现,譬如线程池、信号量等。线程池(thread pool)这个东西,在面试上多次被问到,一般的回答都是:“管理一个任务队列,一个线程队列,然后每次取一个任务分配给一个线程去做,循环往复。” 貌似没有问题吧。但是写起程序来的时候就出问题了。

废话不多说,先上实现,然后再啰嗦。(dont talk, show me ur code !)


 
 
 
  1. #ifndef ILOVERS_THREAD_POOL_H
  2. #define ILOVERS_THREAD_POOL_H
  3.  
  4. #include <iostream>
  5. #include <functional>
  6. #include <thread>
  7. #include <condition_variable>
  8. #include <future>
  9. #include <atomic>
  10. #include <vector>
  11. #include <queue>
  12.  
  13. // 命名空间
  14. namespace ilovers {
  15. class TaskExecutor;
  16. }
  17.  
  18. class ilovers::TaskExecutor{
  19. using Task = std::function<void()>;
  20. private:
  21. // 线程池
  22. std::vector<std::thread> pool;
  23. // 任务队列
  24. std::queue<Task> tasks;
  25. // 同步
  26. std::mutex m_task;
  27. std::condition_variable cv_task;
  28. // 是否关闭提交
  29. std::atomic<bool> stop;
  30. public:
  31. // 构造
  32. TaskExecutor(size_t size = 4): stop { false}{
  33. size = size < 1 ? 1 : size;
  34. for(size_t i = 0
线程池是一种常见的并发编程技术,它可以有效地管理线程,提高程序的并发性能。在 C 语言中,我们可以使用 POSIX 线程库来实现线程池。 以下是一个简单的 C 语言线程池实现: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define MAX_THREADS 10 #define MAX_QUEUE 1000 typedef struct { void (*function)(void *); void *argument; } task_t; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t notify = PTHREAD_COND_INITIALIZER; static task_t task_queue[MAX_QUEUE]; static int queue_head = 0, queue_tail = 0, queue_size = 0; static int shutdown = 0; static pthread_t thread_pool[MAX_THREADS]; static int thread_count = 0; static void *thread_function(void *arg) { while (1) { pthread_mutex_lock(&lock); while (queue_size == 0 && !shutdown) { pthread_cond_wait(&notify, &lock); } if (shutdown) { pthread_mutex_unlock(&lock); pthread_exit(NULL); } void (*task_function)(void *); void *task_argument; task_function = task_queue[queue_head].function; task_argument = task_queue[queue_head].argument; queue_size--; queue_head++; if (queue_head == MAX_QUEUE) { queue_head = 0; } pthread_mutex_unlock(&lock); (*task_function)(task_argument); } return NULL; } int thread_pool_init(int num_threads) { if (num_threads <= 0 || num_threads > MAX_THREADS) { num_threads = MAX_THREADS; } for (int i = 0; i < num_threads; i++) { if (pthread_create(&thread_pool[i], NULL, thread_function, NULL) != 0) { return -1; } thread_count++; } return 0; } int thread_pool_add_task(void (*function)(void *), void *argument) { pthread_mutex_lock(&lock); if (queue_size == MAX_QUEUE) { pthread_mutex_unlock(&lock); return -1; } task_queue[queue_tail].function = function; task_queue[queue_tail].argument = argument; queue_size++; queue_tail++; if (queue_tail == MAX_QUEUE) { queue_tail = 0; } pthread_cond_signal(&notify); pthread_mutex_unlock(&lock); return 0; } int thread_pool_destroy() { pthread_mutex_lock(&lock); shutdown = 1; pthread_cond_broadcast(&notify); pthread_mutex_unlock(&lock); for (int i = 0; i < thread_count; i++) { pthread_join(thread_pool[i], NULL); } return 0; } ``` 使用方法如下: ```c void print_message(void *arg) { char *message = (char *) arg; printf("%s\n", message); free(message); } int main() { thread_pool_init(4); for (int i = 0; i < 8; i++) { char *message = malloc(sizeof(char) * 20); sprintf(message, "Task %d", i); thread_pool_add_task(print_message, message); } thread_pool_destroy(); return 0; } ``` 该示例代码中,我们定义了一个任务结构体 `task_t`,包含了任务函数指针和参数。线程池中维护了一个任务队列,当有任务添加时,将任务加入队列,等待线程池中的线程来执行。线程池的每个线程都会从任务队列中取出一个任务并执行。线程池的销毁通过发送停止信号来实现,即将 `shutdown` 标志设置为 1,并唤醒所有等待条件变量的线程。最后,等待所有线程执行结束,然后退出线程池
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值