写一个linux c线程池并详细解释代码
时间: 2023-04-08 17:02:00 浏览: 101
好的,我可以回答这个问题。线程池是一种常见的多线程编程技术,它可以提高程序的并发性能。下面是一个简单的 Linux C 线程池的实现,代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREAD_NUM 5
#define QUEUE_SIZE 10
typedef struct {
void *(*function)(void *);
void *argument;
} task_t;
typedef struct {
task_t *tasks;
int head;
int tail;
int size;
int count;
pthread_mutex_t lock;
pthread_cond_t notify_producer;
pthread_cond_t notify_consumer;
} queue_t;
typedef struct {
pthread_t *threads;
queue_t *queue;
int thread_count;
int queue_size;
int shutdown;
} threadpool_t;
void queue_init(queue_t *queue, int size) {
queue->tasks = (task_t *)malloc(sizeof(task_t) * size);
queue->head = 0;
queue->tail = 0;
queue->size = size;
queue->count = 0;
pthread_mutex_init(&queue->lock, NULL);
pthread_cond_init(&queue->notify_producer, NULL);
pthread_cond_init(&queue->notify_consumer, NULL);
}
void queue_destroy(queue_t *queue) {
pthread_mutex_destroy(&queue->lock);
pthread_cond_destroy(&queue->notify_producer);
pthread_cond_destroy(&queue->notify_consumer);
free(queue->tasks);
}
void queue_push(queue_t *queue, task_t task) {
pthread_mutex_lock(&queue->lock);
while (queue->count == queue->size) {
pthread_cond_wait(&queue->notify_producer, &queue->lock);
}
queue->tasks[queue->tail] = task;
queue->tail = (queue->tail + 1) % queue->size;
queue->count++;
pthread_cond_signal(&queue->notify_consumer);
pthread_mutex_unlock(&queue->lock);
}
task_t queue_pop(queue_t *queue) {
pthread_mutex_lock(&queue->lock);
while (queue->count == 0) {
pthread_cond_wait(&queue->notify_consumer, &queue->lock);
}
task_t task = queue->tasks[queue->head];
queue->head = (queue->head + 1) % queue->size;
queue->count--;
pthread_cond_signal(&queue->notify_producer);
pthread_mutex_unlock(&queue->lock);
return task;
}
void *threadpool_worker(void *arg) {
threadpool_t *pool = (threadpool_t *)arg;
while (1) {
task_t task = queue_pop(pool->queue);
if (pool->shutdown) {
break;
}
task.function(task.argument);
}
pthread_exit(NULL);
}
void threadpool_init(threadpool_t *pool, int thread_count, int queue_size) {
pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * thread_count);
pool->queue = (queue_t *)malloc(sizeof(queue_t));
queue_init(pool->queue, queue_size);
pool->thread_count = thread_count;
pool->queue_size = queue_size;
pool->shutdown = 0;
for (int i = 0; i < thread_count; i++) {
pthread_create(&pool->threads[i], NULL, threadpool_worker, (void *)pool);
}
}
void threadpool_submit(threadpool_t *pool, task_t task) {
queue_push(pool->queue, task);
}
void threadpool_shutdown(threadpool_t *pool) {
pool->shutdown = 1;
pthread_cond_broadcast(&pool->queue->notify_consumer);
for (int i = 0; i < pool->thread_count; i++) {
pthread_join(pool->threads[i], NULL);
}
queue_destroy(pool->queue);
free(pool->threads);
free(pool->queue);
}
void *task_function(void *arg) {
int *num = (int *)arg;
printf("Task %d is running\n", *num);
return NULL;
}
int main() {
threadpool_t pool;
threadpool_init(&pool, THREAD_NUM, QUEUE_SIZE);
for (int i = 0; i < 20; i++) {
int *num = (int *)malloc(sizeof(int));
*num = i;
task_t task = { task_function, num };
threadpool_submit(&pool, task);
}
threadpool_shutdown(&pool);
return 0;
}
```
这个线程池包含了一个任务队列和一组工作线程。任务队列是一个循环队列,用于存储待执行的任务。工作线程从任务队列中取出任务并执行。当任务队列为空时,工作线程会阻塞等待新的任务。当线程池关闭时,工作线程会退出。
线程池的使用方法如下:
1. 调用 threadpool_init 初始化线程池。
2. 创建任务并调用 threadpool_submit 提交任务。
3. 调用 threadpool_shutdown 关闭线程池。
以上就是一个简单的 Linux C 线程池的实现。
阅读全文