线程池
实现简单的线程池
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NUM_THREADS 5
#define QUEUE_SIZE 10
typedef struct {
void (*function)(void*);
void *argument;
} Task;
typedef struct {
Task task_queue[QUEUE_SIZE];
int queue_front;
int queue_rear;
pthread_mutex_t lock;
pthread_cond_t notify;
pthread_t threads[NUM_THREADS];
int shutdown;
} ThreadPool;
void *thread_do_work(void *pool);
ThreadPool* threadpool_create() {
ThreadPool *pool = (ThreadPool *)malloc(sizeof(ThreadPool));
if (pool == NULL) {
perror("malloc");
return NULL;
}
pool->queue_front = 0;
pool->queue_rear = 0;
pool->shutdown = 0;
pthread_mutex_init(&(pool->lock), NULL);
pthread_cond_init(&(pool->notify), NULL);
for (int i = 0; i < NUM_THREADS; i++) {
pthread_create(&(pool->threads[i]), NULL, thread_do_work, (void*)pool);
}
return pool;
}
void threadpool_add(Task task, ThreadPool *pool) {
pthread_mutex_lock(&(pool->lock));
pool->task_queue[pool->queue_rear] = task;
pool->queue_rear = (pool->queue_rear + 1) % QUEUE_SIZE;
pthread_cond_signal(&(pool->notify));
pthread_mutex_unlock(&(pool->lock));
}
void *thread_do_work(void *pool) {
ThreadPool *thread_pool = (ThreadPool *)pool;
Task task;
while (1) {
pthread_mutex_lock(&(thread_pool->lock));
while ((thread_pool->queue_front == thread_pool->queue_rear) && (!thread_pool->shutdown)) {
pthread_cond_wait(&(thread_pool->notify), &(thread_pool->lock));
}
if (thread_pool->shutdown) {
pthread_mutex_unlock(&(thread_pool->lock));
pthread_exit(NULL);
}
task = thread_pool->task_queue[thread_pool->queue_front];
thread_pool->queue_front = (thread_pool->queue_front + 1) % QUEUE_SIZE;
pthread_mutex_unlock(&(thread_pool->lock));
(*(task.function))(task.argument);
}
pthread_exit(NULL);
return NULL;
}
void threadpool_destroy(ThreadPool *pool) {
pool->shutdown = 1;
pthread_cond_broadcast(&(pool->notify));
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(pool->threads[i], NULL);
}
pthread_mutex_destroy(&(pool->lock));
pthread_cond_destroy(&(pool->notify));
free(pool);
}
void example_task(void *arg) {
int num = *(int *)arg;
printf("Thread %ld is working on task %d\n", pthread_self(), num);
sleep(1);
}
int main() {
ThreadPool *pool = threadpool
用线程池执行任务示例
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define TASKS 10
typedef struct {
void (*function)(void*);
void *argument;
} Task;
typedef struct {
Task *task_queue;
int queue_size;
int queue_front;
int queue_rear;
pthread_mutex_t lock;
pthread_cond_t notify;
pthread_t *threads;
int thread_count;
int shutdown;
} ThreadPool;
void thread_function(void *arg) {
int task_num = *(int *)arg;
printf("Thread %ld is executing task %d\n", pthread_self(), task_num);
sleep(1);
}
ThreadPool *threadpool_create(int thread_count, int queue_size) {
ThreadPool *pool = (ThreadPool *)malloc(sizeof(ThreadPool));
if (pool == NULL) {
perror("malloc");
return NULL;
}
pool->thread_count = thread_count;
pool->threads = (pthread_t *)malloc(thread_count * sizeof(pthread_t));
pool->task_queue = (Task *)malloc(queue_size * sizeof(Task));
pool->queue_size = queue_size;
pool->queue_front = 0;
pool->queue_rear = 0;
pool->shutdown = 0;
pthread_mutex_init(&(pool->lock), NULL);
pthread_cond_init(&(pool->notify), NULL);
for (int i = 0; i < thread_count; i++) {
pthread_create(&(pool->threads[i]), NULL, (void *(*)(void *)) thread_function, NULL);
}
return pool;
}
void threadpool_add_task(ThreadPool *pool, void (*function)(void*), void *argument) {
pthread_mutex_lock(&(pool->lock));
while ((pool->queue_rear + 1) % pool->queue_size == pool->queue_front) {
pthread_cond_wait(&(pool->notify), &(pool->lock));
}
pool->task_queue[pool->queue_rear].function = function;
pool->task_queue[pool->queue_rear].argument = argument;
pool->queue_rear = (pool->queue_rear + 1) % pool->queue_size;
pthread_cond_signal(&(pool->notify));
pthread_mutex_unlock(&(pool->lock));
}
void threadpool_destroy(ThreadPool *pool) {
pool->shutdown = 1;
pthread_cond_broadcast(&(pool->notify));
for (int i = 0; i < pool->thread_count; i++) {
pthread_join(pool->threads[i], NULL);
}
pthread_mutex_destroy(&(pool->lock));
pthread_cond_destroy(&(pool->notify));
free(pool->threads);
free(pool->task_queue);
free(pool);
}
int main() {
ThreadPool *pool = threadpool_create(3, 5);
for (int i = 0; i < TASKS; i++) {
int *task_num = (int *)malloc(sizeof(int));
*task_num = i;
threadpool_add_task(pool, thread_function, (void *)task_num);
}
sleep(5);
threadpool_destroy(pool);
return 0;
}