能帮助我写一个基于C++11的线程池码
时间: 2023-05-28 22:05:28 浏览: 77
以下是一个基于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.
```
阅读全文