使用c++11实现线程池
时间: 2023-05-31 08:03:45 浏览: 73
以下是一个简单的C11线程池实现,包含基本的初始化、添加任务和销毁线程池的函数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdatomic.h>
// 最大任务数
#define MAX_TASKS 1024
// 任务结构体
typedef struct {
void (*func)(void*); // 任务函数指针
void* arg; // 任务参数
} task_t;
// 线程池结构体
typedef struct {
pthread_t* threads; // 线程数组
atomic_int num_threads; // 线程数
atomic_int num_tasks; // 任务数
atomic_bool shutdown; // 关闭标志
pthread_mutex_t lock; // 互斥锁
pthread_cond_t task_cond; // 条件变量
task_t tasks[MAX_TASKS]; // 任务数组
} thread_pool_t;
// 执行任务
static void* thread_func(void* arg) {
thread_pool_t* pool = (thread_pool_t*)arg;
while (1) {
// 等待任务
pthread_mutex_lock(&pool->lock);
while (pool->num_tasks == 0 && !pool->shutdown) {
pthread_cond_wait(&pool->task_cond, &pool->lock);
}
// 关闭
if (pool->shutdown) {
pthread_mutex_unlock(&pool->lock);
pthread_exit(NULL);
}
// 取出任务
task_t task = pool->tasks[--pool->num_tasks];
pthread_mutex_unlock(&pool->lock);
// 执行任务
task.func(task.arg);
}
}
// 初始化线程池
int thread_pool_init(thread_pool_t* pool, int num_threads) {
// 初始化互斥锁和条件变量
if (pthread_mutex_init(&pool->lock, NULL) != 0) {
return -1;
}
if (pthread_cond_init(&pool->task_cond, NULL) != 0) {
pthread_mutex_destroy(&pool->lock);
return -1;
}
// 初始化线程数组
pool->threads = (pthread_t*)malloc(num_threads * sizeof(pthread_t));
if (pool->threads == NULL) {
pthread_mutex_destroy(&pool->lock);
pthread_cond_destroy(&pool->task_cond);
return -1;
}
// 初始化变量
atomic_init(&pool->num_threads, num_threads);
atomic_init(&pool->num_tasks, 0);
atomic_init(&pool->shutdown, 0);
// 创建线程
for (int i = 0; i < num_threads; i++) {
if (pthread_create(&pool->threads[i], NULL, thread_func, pool) != 0) {
thread_pool_destroy(pool);
return -1;
}
}
return 0;
}
// 添加任务
int thread_pool_add_task(thread_pool_t* pool, void (*func)(void*), void* arg) {
if (atomic_load(&pool->shutdown)) {
return -1;
}
// 添加任务
pthread_mutex_lock(&pool->lock);
if (pool->num_tasks == MAX_TASKS) {
pthread_mutex_unlock(&pool->lock);
return -1;
}
task_t task = { func, arg };
pool->tasks[pool->num_tasks++] = task;
pthread_cond_signal(&pool->task_cond);
pthread_mutex_unlock(&pool->lock);
return 0;
}
// 销毁线程池
void thread_pool_destroy(thread_pool_t* pool) {
if (atomic_exchange(&pool->shutdown, 1)) {
return;
}
// 唤醒所有等待任务的线程
pthread_mutex_lock(&pool->lock);
pthread_cond_broadcast(&pool->task_cond);
pthread_mutex_unlock(&pool->lock);
// 等待所有线程退出
for (int i = 0; i < atomic_load(&pool->num_threads); i++) {
pthread_join(pool->threads[i], NULL);
}
// 释放资源
free(pool->threads);
pthread_mutex_destroy(&pool->lock);
pthread_cond_destroy(&pool->task_cond);
}
```
使用示例:
```c
void task_func(void* arg) {
int* num = (int*)arg;
printf("Task %d executed by thread %ld\n", *num, pthread_self());
}
int main() {
thread_pool_t pool;
thread_pool_init(&pool, 4);
int nums[10];
for (int i = 0; i < 10; i++) {
nums[i] = i + 1;
thread_pool_add_task(&pool, task_func, &nums[i]);
}
thread_pool_destroy(&pool);
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)