在Linux环境下使用C语言进行多线程编程时,线程池是一种常见的资源管理方式,它可以有效地控制并发线程的数量,减少线程创建和销毁的开销。线程池中的线程在执行完任务后不会立即销毁,而是返回线程池等待下一个任务。
线程池回收涉及的基础概念:
线程池回收的优势:
线程池回收的类型:
应用场景:
线程池回收遇到的问题及原因:
示例代码:
以下是一个简单的线程池实现示例:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX_THREADS 10
#define MAX_QUEUE 100
typedef struct {
void (*function)(void *);
void *argument;
} task_t;
typedef struct {
task_t queue[MAX_QUEUE];
int head;
int tail;
int count;
pthread_mutex_t lock;
pthread_cond_t notify;
pthread_t threads[MAX_THREADS];
int shutdown;
} thread_pool_t;
void *thread_pool_worker(void *arg) {
thread_pool_t *pool = (thread_pool_t *)arg;
while (1) {
pthread_mutex_lock(&pool->lock);
while (pool->count == 0 && !pool->shutdown) {
pthread_cond_wait(&pool->notify, &pool->lock);
}
if (pool->shutdown) {
pthread_mutex_unlock(&pool->lock);
pthread_exit(NULL);
}
task_t task = pool->queue[pool->head];
pool->head = (pool->head + 1) % MAX_QUEUE;
pool->count--;
pthread_mutex_unlock(&pool->lock);
task.function(task.argument);
}
}
int thread_pool_init(thread_pool_t *pool) {
pool->head = 0;
pool->tail = 0;
pool->count = 0;
pool->shutdown = 0;
pthread_mutex_init(&pool->lock, NULL);
pthread_cond_init(&pool->notify, NULL);
for (int i = 0; i < MAX_THREADS; i++) {
pthread_create(&pool->threads[i], NULL, thread_pool_worker, pool);
}
return 0;
}
int thread_pool_add_task(thread_pool_t *pool, void (*function)(void *), void *argument) {
pthread_mutex_lock(&pool->lock);
if (pool->count == MAX_QUEUE) {
pthread_mutex_unlock(&pool->lock);
return -1; // Queue is full
}
pool->queue[pool->tail].function = function;
pool->queue[pool->tail].argument = argument;
pool->tail = (pool->tail + 1) % MAX_QUEUE;
pool->count++;
pthread_cond_signal(&pool->notify);
pthread_mutex_unlock(&pool->lock);
return 0;
}
void thread_pool_destroy(thread_pool_t *pool) {
pool->shutdown = 1;
pthread_cond_broadcast(&pool->notify);
for (int i = 0; i < MAX_THREADS; i++) {
pthread_join(pool->threads[i], NULL);
}
pthread_mutex_destroy(&pool->lock);
pthread_cond_destroy(&pool->notify);
}
void example_task(void *arg) {
printf("Task executed with argument: %s
", (char *)arg);
}
int main() {
thread_pool_t pool;
thread_pool_init(&pool);
for (int i = 0; i < 20; i++) {
char *arg = malloc(10);
sprintf(arg, "Task %d", i);
thread_pool_add_task(&pool, example_task, arg);
}
sleep(1); // Wait for tasks to complete
thread_pool_destroy(&pool);
return 0;
}
在这个示例中,我们创建了一个固定大小的线程池,并添加了一些任务。线程池中的线程会从任务队列中取出任务并执行。通过这种方式,我们可以有效地管理和复用线程资源。
领取专属 10元无门槛券
手把手带您无忧上云