【无标题】

线程池

实现简单的线程池

#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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值