c++ 11 线程池代码实现
时间: 2023-10-04 22:11:40 浏览: 164
以下是一个简单的C语言线程池的代码实现:
```c
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define DEFAULT_THREADS 4
typedef struct {
void (*function)(void *);
void *argument;
} thread_task;
typedef struct {
thread_task *task_queue;
int queue_size;
int front;
int rear;
int count;
pthread_mutex_t lock;
pthread_cond_t not_empty;
pthread_cond_t not_full;
int threads_count;
pthread_t *threads;
int shutdown;
} thread_pool;
void *thread_pool_function(void *thread_pool_ptr) {
thread_pool *pool = (thread_pool *) thread_pool_ptr;
thread_task task;
while (1) {
pthread_mutex_lock(&(pool->lock));
while (pool->count == 0 && !pool->shutdown) {
pthread_cond_wait(&(pool->not_empty), &(pool->lock));
}
if (pool->shutdown) {
pthread_mutex_unlock(&(pool->lock));
pthread_exit(NULL);
}
task = pool->task_queue[pool->front];
pool->front = (pool->front + 1) % pool->queue_size;
pool->count--;
if (pool->count == pool->queue_size - 1) {
pthread_cond_broadcast(&(pool->not_full));
}
pthread_mutex_unlock(&(pool->lock));
(*(task.function))(task.argument);
}
pthread_exit(NULL);
}
int thread_pool_create(thread_pool *pool, int threads_count) {
int i;
pool->threads_count = 0;
pool->shutdown = 0;
if (threads_count <= 0) {
threads_count = DEFAULT_THREADS;
}
pool->queue_size = threads_count * 2;
pool->front = 0;
pool->rear = 0;
pool->count = 0;
pool->task_queue = (thread_task *) malloc(sizeof(thread_task) * pool->queue_size);
pool->threads = (pthread_t *) malloc(sizeof(pthread_t) * threads_count);
pthread_mutex_init(&(pool->lock), NULL);
pthread_cond_init(&(pool->not_empty), NULL);
pthread_cond_init(&(pool->not_full), NULL);
for (i = 0; i < threads_count; i++) {
pthread_create(&(pool->threads[i]), NULL, thread_pool_function, (void *) pool);
pool->threads_count++;
}
return 0;
}
int thread_pool_add_task(thread_pool *pool, void (*function)(void *), void *argument) {
thread_task task;
pthread_mutex_lock(&(pool->lock));
while (pool->count == pool->queue_size && !pool->shutdown) {
pthread_cond_wait(&(pool->not_full), &(pool->lock));
}
if (pool->shutdown) {
pthread_mutex_unlock(&(pool->lock));
return -1;
}
task.function = function;
task.argument = argument;
pool->task_queue[pool->rear] = task;
pool->rear = (pool->rear + 1) % pool->queue_size;
pool->count++;
if (pool->count == 1) {
pthread_cond_broadcast(&(pool->not_empty));
}
pthread_mutex_unlock(&(pool->lock));
return 0;
}
int thread_pool_destroy(thread_pool *pool) {
int i;
if (pool->shutdown) {
return -1;
}
pool->shutdown = 1;
pthread_cond_broadcast(&(pool->not_empty));
pthread_cond_broadcast(&(pool->not_full));
for (i = 0; i < pool->threads_count; i++) {
pthread_join(pool->threads[i], NULL);
}
free(pool->task_queue);
free(pool->threads);
pthread_mutex_destroy(&(pool->lock));
pthread_cond_destroy(&(pool->not_empty));
pthread_cond_destroy(&(pool->not_full));
return 0;
}
void task_function(void *argument) {
int *value = (int *) argument;
printf("Task %d\n", *value);
}
int main() {
int i;
int values[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
thread_pool pool;
thread_pool_create(&pool, 4);
for (i = 0; i < 10; i++) {
thread_pool_add_task(&pool, task_function, &values[i]);
}
thread_pool_destroy(&pool);
return 0;
}
```
该实现使用一个循环队列来存储线程任务,线程池的每个线程都会不断地从队列中取出任务并执行。线程池的创建、添加任务和销毁都是线程安全的,确保了多线程环境下线程池的正确性。在本例中,我们创建了一个包含10个任务的线程池,每个任务打印一个数字。
阅读全文