如何用C语言实现线程池
时间: 2023-05-19 08:07:05 浏览: 136
实现线程池的基本步骤如下:
1. 创建一个任务队列,用于存放需要执行的任务。
2. 创建一定数量的线程,这些线程会从任务队列中取出任务并执行。
3. 当有新的任务需要执行时,将任务添加到任务队列中。
4. 当线程执行完任务后,继续从任务队列中取出任务执行,直到任务队列为空。
下面是一个简单的线程池实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREAD_NUM 5
typedef struct task {
void (*func)(void *arg);
void *arg;
struct task *next;
} task_t;
typedef struct thread_pool {
pthread_mutex_t lock;
pthread_cond_t cond;
task_t *head;
int thread_count;
int queue_size;
int shutdown;
pthread_t *threads;
} thread_pool_t;
void *thread_func(void *arg)
{
thread_pool_t *pool = (thread_pool_t *)arg;
task_t *task;
while (1) {
pthread_mutex_lock(&pool->lock);
while (pool->queue_size == 0 && !pool->shutdown) {
pthread_cond_wait(&pool->cond, &pool->lock);
}
if (pool->shutdown) {
pthread_mutex_unlock(&pool->lock);
pthread_exit(NULL);
}
task = pool->head;
pool->head = task->next;
pool->queue_size--;
pthread_mutex_unlock(&pool->lock);
(*(task->func))(task->arg);
free(task);
}
return NULL;
}
int thread_pool_init(thread_pool_t *pool, int thread_count)
{
int i;
pool->thread_count = 0;
pool->queue_size = 0;
pool->shutdown = 0;
pool->head = NULL;
if (pthread_mutex_init(&pool->lock, NULL) != 0) {
return -1;
}
if (pthread_cond_init(&pool->cond, NULL) != 0) {
pthread_mutex_destroy(&pool->lock);
return -1;
}
pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * thread_count);
for (i = 0; i < thread_count; i++) {
if (pthread_create(&pool->threads[i], NULL, thread_func, (void *)pool) != 0) {
thread_pool_destroy(pool);
return -1;
}
pool->thread_count++;
}
return 0;
}
int 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_size == 0) {
pool->head = task;
} else {
task_t *p = pool->head;
while (p->next != NULL) {
p = p->next;
}
p->next = task;
}
pool->queue_size++;
pthread_cond_signal(&pool->cond);
pthread_mutex_unlock(&pool->lock);
return 0;
}
int thread_pool_destroy(thread_pool_t *pool)
{
int i;
task_t *task;
if (pool->shutdown) {
return -1;
}
pool->shutdown = 1;
pthread_cond_broadcast(&pool->cond);
for (i = 0; i < pool->thread_count; i++) {
pthread_join(pool->threads[i], NULL);
}
free(pool->threads);
while (pool->head != NULL) {
task = pool->head;
pool->head = task->next;
free(task);
}
pthread_mutex_destroy(&pool->lock);
pthread_cond_destroy(&pool->cond);
return 0;
}
void print_hello(void *arg)
{
int *p = (int *)arg;
printf("Hello, %d!\n", *p);
}
int main()
{
thread_pool_t pool;
int i, args[10];
thread_pool_init(&pool, THREAD_NUM);
for (i = 0; i < 10; i++) {
args[i] = i;
thread_pool_add_task(&pool, print_hello, &args[i]);
}
sleep(1);
thread_pool_destroy(&pool);
return 0;
}
```
这个线程池实现了一个简单的任务队列,可以添加任务并由多个线程执行。当线程池被销毁时,所有线程都会退出,并且任务队列中的所有任务都会被清空。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)