线程池简单实现

//线程池的需求分析
//1.初始化线程池的空间
//2.每个线程处理函数其实都是轮询在处理队列
//3.addWorkers其实就是在往队列里面放任务
//4.线程池销毁就是把启动的几个线程干死就好了

typedef struct _task {
    void *(*process) (void *arg);
    void *arg;
    struct _task *next;
} task;

typedef struct _thread_pool {
    //互斥锁
    pthread_mutex_t queue_lock;
    //条件变量
    pthread_cond_t queue_ready;
    //任务队列指针
    worker *queue_head;
    //是否销毁线程池
    int shutdown;
    //线程id数组
    pthread_t *thread_id;
    //线程池中最多允许活动线程数目
    int max_thread_num;
    //当前队列的任务个数
    int cur_queue_size;
} thread_pool;

//添加任务到队列中
int pool_add_worker(void *(*process) (void *arg), void *arg);
//线程路由
void *thread_routine(void *arg);

static thread_pool *pool = NULL;

//线程池,锁等初始化
void pool_init(int max_thread_num) {
    //为线程池分配内存
    pool = (thread_pool *)malloc(sizeof(thread_pool));

    //互斥量初始化
    pthread_mutex_init(&(pool->queue_lock), NULL);
    //条件变量初始化
    pthread_cond_init(&(pool_queue_ready), NULL);

    pool->queue_head = NULL;
    pool->max_thread_num = max_thread_num;
    pool->cur_queue_size = 0;
    pool->shutdown = 0;
    pool->thread_id = (pthread_t *) malloc(sizeof(pthread_t) * max_thread_num);

    int i = 0;
    for (i = 0; i < max_thread_num; i++) {
        pthread_create(&(pool->thread_id[i]), NULL, thread_routine, NULL);
    }

}


int pool_add_worker(void *(*process) (void *arg), void *arg) {
    //一个新任务
    worker *new_worker = (worker *)malloc(sizeof(worker));
    new_worker->process = process;
    new_worker->arg = arg;
    new_worker->next = NULL;

    //来,互斥量上
    pthread_mutex_lock(&(pool->queue_lock));

    //这里其实就是一个单链表的插入操作,过程也是极其的简单
    worker *member = pool->queue_head;
    if (member != NULL) {
        while (member->next != NULL) {
            member = member->next;
        }
        member->next = new_worker;

    } else {
        pool->queue_head = new_worker;
    }

    assert(pool->queue_head != NULL);
    pool->cur_queue_size++;

    pthread_mutex_unlock(&(pool->queue_lock));
    //激活在等待状态的线程起床工作了
    pthread_cond_signal(&(pool->queue_ready));
    return 0;
}

int pool_destroy() {

    if (pool->shutdown) {
        return -1;
    }

    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));

    for (int i = 0; i < pool->max_thread_num; i++) {
        pthread_join(pool->thread_id[i], NULL);
    }

    free(pool->thread_id);
    //释放队列
    worker *head = NULL;
    while (pool->queue_head != NULL) {
        head = pool->queue_head;
        pool->queue_head = pool->queue_head->next;
        free(head);
    }

    //互斥量销毁
    pthread_mutex_destroy(&(pool->queue_lock));
    //销毁条件变量
    pthread_cond_destroy(&(pool->queue_ready));

    free(pool);
    pool = NULL;

    return 0;
}

void * thread_routine(void *arg) {
    while (1) {
        pthread_mutex_lock(&(pool->queue_lock));

        while (pool->cur_queue_size == 0 && !pool->shutdown) {
            pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock));
        }

        //线程要销毁了
        if (pool->shutdown) {

        }

        assert(pool->cur_queue_size != 0);
        assert(pool->queue_head != NULL);

        pool->cur_queue_size--;
        worker *worker = pool->queue_head;
        pool->queue_head = worker->next;

        pthread_mutex_unlock (&(pool->queue_lock));
        /*调用回调函数,执行任务*/
        (*(worker->process)) (worker->arg);
        free (worker);
        worker = NULL;

    }

    pthread_exit(NULL);
}


int main (int argc, char **argv)
{
    pool_init (3);/*线程池中最多三个活动线程*/

    /*连续向池中投入10个任务*/
    int *workingnum = (int *) malloc (sizeof (int) * 10);
    int i;
    for (i = 0; i < 10; i++)
    {
        workingnum[i] = i;
        pool_add_worker (myprocess, &workingnum[i]);
    }
    /*等待所有任务完成*/
    sleep (5);
    /*销毁线程池*/
    pool_destroy ();
    free (workingnum);
    return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值