linux下c语言实现简单----线程池

这两天刚好看完linux&c这本书的进程线程部分,学长建议可以用c语言实现一个简单的线程池,也是对线程知识的一个回顾与应用。线程的优点有好多,它是”轻量级的进程”,所需资源少,多线程共享数据空间,线程之间切换速度更快,可以减少服务器的闲置时间等。

那么我们要在往高层次讲它也有一些缺点,比如一个多线程的程序在创建和销毁线程的时候是比较费资源的,举一个例子,我们要是自己是一个普通饭店老板,当顾客来了我们再做饭,那么势必比较慢,这当然会使得顾客感受不好;但是我们要是KFC工作人员,来一个顾客,马上就能为他服务,这是因为我们提前准备好了汉堡。而线程池也正是这个原理,我们提前创建线程,让它们处在等待状态,要是有任务,就激活线程,执行完毕要是没有任务就继续等待。下面是IBM上对线程池背景介绍

线程池的技术背景
在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源。在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收。所以提高服务程序效率的一个手段就是尽可能减少创建和销毁对象的次数,特别是一些很耗资源的对象创建和销毁。如何利用已有对象来服务就是一个需要解决的关键问题,其实这就是一些”池化资源”技术产生的原因。比如大家所熟悉的数据库连接池正是遵循这一思想而产生的。

下面是我实现的代码:我的先城池大小为5,代码中我为它添加了10个任务。

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>

typedef void*(*FUNC)(void* arg);                //指向函数的指针,用来指向我的线程函数。
typedef struct _thpool_jobqueue                 //任务队列
{
    FUNC                     routine;           //定义指向线程函数的指针routine
    void*                    arg;               //传向线程函数的参数
    struct _thpool_jobqueue  *next;
}thpool_jobqueue;
typedef struct _thpool_t                        //线程池
{
    int               max_thr_num;              //线程池的尺寸
    int               flag;                     //是否销毁的标志
    pthread_t         *thr_id;                  //线程ID指针
    pthread_cond_t    jobqueue_cond;            //条件变量
    pthread_mutex_t   jobqueue_mutex;           //互斥锁
    thpool_jobqueue   *jobqueue_head;           //指向任务队列的指针
}thpool_t;
static thpool_t * thpool = NULL;                //全局变量,指向线程池的指针

/*建议:为了结构更清晰,请从main函数开始,然后线程创建函数--->任务添加函数--->线程函数*/


void *thread_routine()                          //线程函数
{
    thpool_jobqueue *work;                        
    while(1)
    {
        pthread_mutex_lock(&thpool->jobqueue_mutex);              
        while((thpool->jobqueue_head == NULL) &&( thpool->flag == 0)) //如果此时没有任务,并且不打算销毁线程池
        {
            pthread_cond_wait(&thpool->jobqueue_cond,&thpool->jobqueue_mutex);//       就让抢到锁的在此等待,其他线程在锁的外边阻塞
        }
        if(thpool->flag != 0)                                         //每次可以执行的时候都判断下线程池是否要销毁
        {
            pthread_mutex_unlock(&thpool->jobqueue_mutex);
            pthread_exit(0);
        }
        work = thpool->jobqueue_head;                                 //若不销毁,则将任务添加到任务队列
        thpool->jobqueue_head = thpool->jobqueue_head->next;          //让任务指针指向下一个
        pthread_mutex_unlock(&thpool->jobqueue_mutex);
        work->routine(work->arg);                                     //work->routine = routine = func1
        free(work);
    }
}

void thpool_add_task(void*(*routine)(void *),void* arg)        //任务添加函数
{
    thpool_jobqueue  *work,*member;

    work = (thpool_jobqueue*)malloc(sizeof(thpool_jobqueue));  //准备任务,将添加到任务队列
    work->routine = routine;
    work->arg = arg;
    work->next = NULL;
    pthread_mutex_lock(&thpool->jobqueue_mutex);               //对任务队列操作必须保证只有一个线程
    member = thpool->jobqueue_head;
    if(!member)
    {
        thpool->jobqueue_head = work;                          //如果此任务是第一个任务
    }
    else
    {
        while(member->next != NULL)                            //如果不是第一个任务就添加到最后
            member = member->next;
        member->next = work;
    }
    pthread_cond_signal(&thpool->jobqueue_cond);
    pthread_mutex_unlock(&thpool->jobqueue_mutex);
}

void thpool_create(int max_thr_num)                   //线程创建函数
{
    int i;   
    thpool = (thpool_t *)malloc(sizeof(thpool_t));    
    if(!thpool)
    {
        perror("malloc thpool error");
    }
    thpool->flag = 0;                                 //初始化的过程
    thpool->max_thr_num = max_thr_num;                
    thpool->thr_id = (pthread_t*)malloc(max_thr_num*sizeof(pthread_t));
    thpool->jobqueue_head = NULL;
    pthread_mutex_init(&thpool->jobqueue_mutex,NULL);
    pthread_cond_init(&thpool->jobqueue_cond,NULL);
    for(i = 0;i < max_thr_num;i++)
    {
        pthread_create(&thpool->thr_id[i],NULL,thread_routine,NULL);  //创建线程的过程
    }
}
void thpool_destroy()                            //销毁线程池
{
    printf("线程池正在销毁\n");
    int i;
    thpool_jobqueue *member;  
    if(thpool->flag != 0)                        //先判断一下销毁标志是否已经销毁
    {
        return;
    }
    thpool->flag = 1;                            //将销毁标志至为1,即需要销毁
    pthread_mutex_lock(&thpool->jobqueue_mutex); 
    pthread_cond_broadcast(&thpool->jobqueue_cond);  //广播所有线程要销毁的通知
    pthread_mutex_unlock(&thpool->jobqueue_mutex);   
    for(i = 0;i < thpool->max_thr_num;i++)          
    {
        pthread_join(thpool->thr_id[i],NULL);     //等待所有线程都结束
    }
        free(thpool->thr_id);                      //将所有线程ID释放
    while(thpool->jobqueue_head)
    {
        member = thpool->jobqueue_head;
        thpool->jobqueue_head = thpool->jobqueue_head->next;
        free(member);                              //释放每一个任务
    }
    pthread_mutex_destroy(&thpool->jobqueue_mutex);//销毁锁
    pthread_cond_destroy(&thpool->jobqueue_cond);  //销毁条件变量
    free(thpool);                                  //销毁指向线程池的指针
    printf("销毁完成\n");

}

void *func1()
{
    printf("thread %u is running\n",pthread_self());
    sleep(3);                                     //让别的线程有机会抢锁
}

int main(int argc,char *argv[])
{
    int i;

    thpool_create(5);                             //在线程池里创建5个线程
    for(i = 0;i < 10;i++)
    {
        thpool_add_task(func1,NULL);
    }
    sleep(10);
    thpool_destroy();
}
下面是运行结果:
[kiosk@yangbodong 20150805]$ ./a.out 
thread 3508193024 is running
thread 3491407616 is running
thread 3499800320 is running
thread 3516585728 is running
thread 3483014912 is running
thread 3491407616 is running
thread 3508193024 is running
thread 3483014912 is running
thread 3516585728 is running
thread 3499800320 is running
线程池正在销毁
销毁完成

从运行结果中的进程号可以看出总共有5个线程在工作,任务分为两次被执行,前五个running是一次,后五个running是一次,最后完成对线程池的销毁。

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-08-05 19:16  杨博东的博客  阅读(230)  评论(0编辑  收藏  举报

导航