Linux下创建线程池

来源:互联网 发布:小恶魔提利昂知乎 编辑:程序博客网 时间:2024/06/08 01:21

Linux下创建线程池

这里写图片描述
1.

#include <stdio.h>#include <malloc.h>#include <pthread.h>#include <assert.h>pthread_t *pthread_id;typedef struct MyStruct{    void *(*function)(void *arg);    void * arg;    struct work_struct * next;}work_struct;typedef struct  pthread{    //线程锁    pthread_mutex_t queue_lock;    pthread_cond_t queue_ready;    //定义最大线程数    int max_pthread;    //等待工作的链表    work_struct *work_list;    //等待工作的数目    int work_num;    //关闭线程的标志    int flag;    //线程ID    pthread_t *pthreadid;}pthread_pool;static pthread_pool *pool = NULL;//线程入口函数void* function_entry(void *arg){    printf("the is pthread ox%x\n", pthread_self());    while (1)    {        //加锁        pthread_mutex_lock(&(pool->queue_lock));        /*        *等待队列为0,flag为0----进程阻塞        *flad为非0---------------进程退出        *等待队列不为0,flag为0--创建工作        */        //1.进程阻塞        while (pool->work_num == 0 && pool->flag == 0)        {            printf("the pthread is ox%x will wait\n", pthread_self());            pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock));        }        //2.进程退出        if (pool->flag!=0)        {            //先解锁            pthread_mutex_unlock(&(pool->queue_lock));            printf("the pthread will exit ox%x \n",pthread_self());            //线程退出            pthread_exit(NULL);        }        //3.创建工作        printf("the pthread will to work ox%x\n",pthread_self());        //判断是否满足条件        assert(pool->work_num!=0);        assert(pool->work_list!=NULL);        //取出链表元素        pool->work_num--;        work_struct *work = pool->work_list;        pool->work_list = work->next;        pthread_mutex_unlock(&(pool->queue_lock));        //执行函数        (*(work->function))(work->arg);        free(work);        work = NULL;    }}void creat_pthreadpool(int num){    int i;    //分配线程池空间    pool = (pthread_pool *)malloc(sizeof(pthread_pool));    //初始化锁和环境变量    pthread_mutex_init(&(pool->queue_lock),NULL);    pthread_cond_init(&(pool->queue_ready),NULL);    //初始化结构    pool->flag = 0;    pool->max_pthread = num;    pool->work_list = NULL;    pool->work_num = 0;    //分配线程空间    pool->pthreadid = (pthread_t *)malloc(sizeof(pthread_t)*num);    //创建线程    for (i = 0;i < num;i++)    {        pthread_create(&(pool->pthreadid[i]),NULL,function_entry,NULL);    }}void join_work(void *(*function)(void *arg),void *arg){    //构造新任务    work_struct *new_work = (work_struct *)malloc(sizeof(work_struct));    new_work->arg = arg;    new_work->function = function;    new_work->next = NULL;    pthread_mutex_lock(&(pool->queue_lock));    //将任务加入到等待队列    work_struct *tmpwork = pool->work_list;//取出头结点    if (tmpwork!=NULL)    {        while (tmpwork->next!=NULL)        {            tmpwork = tmpwork->next;        }        tmpwork->next = new_work;    }    else    {        pool->work_list = new_work;    }    pool->work_num++;    assert(pool->work_num!=0);    assert(pool->work_list != NULL);    pthread_mutex_unlock(&(pool->queue_lock));    //唤醒等待中的线程    pthread_cond_signal(&(pool->queue_ready));}void close_pool(){    int i;    //标志置1    pool->flag = 1;    //唤醒所有等待中的线程    pthread_cond_broadcast(&(pool->queue_ready));    //等待正在执行的线程    for (i = 0; i < pool->max_pthread; i++)    {        pthread_join(pool->pthreadid[i],NULL);    }    free(pool->pthreadid);    //销毁队列    work_struct *head = NULL;    while (pool->work_list!=NULL)    {        head = pool->work_list;        pool->work_list = head->next;        free(head);    }    //销毁互斥锁和条件变量    pthread_mutex_destroy(&(pool->queue_lock));    pthread_cond_destroy(&(pool->queue_ready));    free(pool);//由malloc创建    //置空指针    pool = NULL;}void print_t(int i){    printf("the pthread is 0x%x,work is %d\n",pthread_self(),i);}void main(){    int i;    //1.创建线程池    creat_pthreadpool(5);    //2.加入任务    for (i = 0;i < 10;i++)    {        join_work(print_t,&i);    }    sleep(5);    //3.关闭线程池    close_pool();}

程序运行结果图:
结果图

我尝试采用pthread_join代替sleep,结果失败了,pthread_join就一直得不到返回,希望解决此问题的大神留言下,谢谢

参考博客:http://blog.csdn.net/zouxinfox/article/details/3560891
http://blog.csdn.net/ithomer/article/details/6063067
http://blog.csdn.net/ithomer/article/details/5921003
http://blog.csdn.net/ithomer/article/details/5920936

0 0