线程池实例

实例一
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <assert.h>

typedef struct worker   //运行和等待的任务链表结构
{
  void *(*process) (void *arg);   //任务运行时会调用该函数
  void *arg;
  struct worker *next;
} CThread_worker;

typedef struct        //线程池结构
{
  pthread_mutex_t queue_lock;
  pthread_cond_t queue_ready;
  CThread_worker *queue_head;    //任务链表结构,用于存储线程池中所有的等待任务
  int shutdown;                  //标志符,用于表示是否销毁线程池,1代表销毁,反之0
  pthread_t *threadid;
  int max_thread_num;     //线程池中允许活动的线程数目
  int cur_queue_size;     //当前任务链表的数目
} CThread_pool;

int pool_add_worker (void *(*process) (void *arg), void *arg);   //向线程池中添加一个任务

void *thread_routine (void *arg);   //线程体函数
static CThread_pool *pool = NULL;

void
pool_init (int max_thread_num)    //预先创建max_thread_num个线程
{
  pool = (CThread_pool *) malloc (sizeof (CThread_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->threadid = (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));
  int i = 0;
  for (i = 0; i < max_thread_num; i++)  //创建新线程
    {
      pthread_create (&(pool->threadid[i]), NULL, thread_routine, NULL);
    }
}

int
pool_add_worker (void *(*process) (void *arg), void *arg)
{
  CThread_worker *newworker =
    (CThread_worker *) malloc (sizeof (CThread_worker));
  newworker->process = process;
  newworker->arg = arg;
  newworker->next = NULL;

  pthread_mutex_lock (&(pool->queue_lock));
  CThread_worker *member = pool->queue_head;  //定义一个新任务并且初始化为第一个任务链表结构
 //创建一个任务队列
  if (member != NULL)
    {
      while (member->next != NULL)
        member = member->next;
      member->next = newworker;
    }
  else
    {
      pool->queue_head = newworker;
    }

  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)); //唤醒所有等待线程
  int i;
  for (i = 0; i < pool->max_thread_num; i++)
    {
      pthread_join (pool->threadid[i], NULL); //阻塞等待所有线程退出
    }
  free (pool->threadid);

  CThread_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)  //线程体函数
{
  printf ("starting thread 0x%x\n", pthread_self ());
  while (1)
    {
      pthread_mutex_lock (&(pool->queue_lock));
      while (pool->cur_queue_size == 0 && !pool->shutdown)
        {
          printf ("thread 0x%x is waiting\n", pthread_self ());
          pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock)); //所有预先创建的线程阻塞等待条件成立
        }
      if (pool->shutdown)  //如果打算销毁线程
        {
          pthread_mutex_unlock (&(pool->queue_lock));
          printf ("thread 0x%x will exit \n", pthread_self ());
          pthread_exit (NULL);
        }

      printf ("thread 0x%x is starting to work\n", pthread_self ());

      assert (pool->cur_queue_size != 0);
      assert (pool->queue_head != NULL);
      pool->cur_queue_size--;
      CThread_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);

}

//以下是测试代码
void *myprocess (void *arg)
{
        printf ("thread is 0x%x, working on task %d\n", pthread_self (), *(int *) arg);
        sleep (1);
        return NULL;
}

int main (int ac, char *av[])
{
        pool_init (3);  //预先创建3个活动线程

        //投入十个任务
        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;
}


实例二

typedef struct _thread_pool_t

{

    pthread_mutex_t  queue_lock ;//任务锁

    pthread_cond_t   task_cond  ;//条件变量

    list_t         * tasks      ;// 任务队列

    pthread_t      * pthreads   ;//线程池

    int              isdestoried;

    int              workersnum ;

    char             ready      ;//指定当前是否存在任务(0,1),pthreads任务队列是否可用

    thread_task_handler  thread_pool_task_handler;//回调函数,线程执行体=mc_thread_entry

}thread_pool_t;


void *thread_entry( void *args )

{

        int fd = *(int *)args ;

        do_handler_fd( fd );

}


void mc_thread_pool_ini( mc_thread_pool_t * par_tp , int workersnum ,thread_task_handler par_handler )  //创建线程池

{

    int err ;

    //par_tp = ( thread_pool_t *)malloc( sizeof(thread_pool_t) );  

    if( par_tp == NULL )

    {

        fprintf( stderr , "thread_pool_t malloc\n");

        return  ;

    }

    par_tp->workersnum = workersnum ;

     

    pthread_mutex_init( &par_tp->queue_lock ,NULL );

    pthread_cond_init(&par_tp->task_cond , NULL ); 

    /*

    par_tp->queue_lock = PTHREAD_MUTEX_INITIALIZER ;

    par_tp->task_cond  = PTHREAD_COND_INITIALIZER  ;

    */

    par_tp->tasks = mc_listcreate() ;

    if( par_tp->tasks == NULL )

    {

        fprintf( stderr , "listcreate() error\n");

        //free( par_tp ) ;

        return  ;

    } 

    par_tp->pthreads = ( pthread_t *)malloc( sizeof( pthread_t )*workersnum );

    if( par_tp->pthreads == NULL )

    {

        fprintf( stderr , "pthreads malloc\n");

        //free( par_tp );

        mc_freelist( par_tp->tasks ) ;

        return NULL ;

    }

    int i = 0 ;

    for( ; i < workersnum ; i++ )

    {

        fprintf(stderr,"start to create threads\n");

        err = pthread_create(&(par_tp->pthreads[i]),NULL,mc_thread_entry,NULL) ;

        if( err == -1 )

        {

            fprintf( stderr , "pthread_create error\n");

            //free( par_tp );

            mc_freelist( par_tp->tasks ) ;

            free(par_tp->pthreads) ;

        }

    } 

    par_tp->thread_pool_task_handler = par_handler ;

    par_tp->ready = 0 ;

    fprintf(stderr,"successed to create threads\n");

}


static void *mc_thread_entry( void *args )    //工作线程

{

    void * task ;

    for(;;)

    {

        pthread_mutex_lock( &mc_global_threads_pool.queue_lock ) ;

        fprintf(stderr, " locked to wait task\n");

        while( mc_global_threads_pool.ready == 0 )

        {

            pthread_cond_wait( &mc_global_threads_pool.task_cond , &mc_global_threads_pool.queue_lock ) ;

        }

        task = mc_thread_pool_get_task() ;

        fprintf(stderr, "get a task and ready to unlock \n");

        pthread_mutex_unlock( &mc_global_threads_pool.queue_lock ) ;

        mc_global_threads_pool.thread_pool_task_handler( task ) ;

    }

}



void mc_thread_pool_add_task(void *task , size_t tasksize ) //写任务队列        {

    pthread_mutex_lock( &mc_global_threads_pool.queue_lock );

    fprintf( stderr ,"thread locked and append to list\n");    

    mc_list_append( mc_global_threads_pool.tasks , task , tasksize ) ;   

    pthread_mutex_unlock( &mc_global_threads_pool.queue_lock );   

    fprintf( stderr ,"thread unlocked and successed append to list\n");     

    mc_global_threads_pool.ready = 1 ;    

    if( mc_global_threads_pool.ready == 1 )

    {

        fprintf( stderr ,"signal to threads\n");

        pthread_cond_signal( &mc_global_threads_pool.task_cond ) ;

    }

}


void *mc_thread_pool_get_task()

{

    void * ret_task ;

    ret_task = mc_getnode_del( mc_global_threads_pool.tasks , 0 );

    if( ret_task == NULL )

    {

        fprintf(stderr,"get node_del error\n");

    }

    fprintf( stderr ," got a task\n");

    mc_global_threads_pool.ready = 0 ;

    if( ret_task == NULL )

    {

        fprintf(stderr, "getnode_del error\n");

        return NULL ;

    }

    else

        return ret_task ;

}


int main()

{

    mc_thread_task_t ltask;

    ltask.task_num = 1 ;

    fprintf(stderr,"begin to ini pool\n");

    mc_thread_pool_ini( &mc_global_threads_pool , 20 , my_thread_task_handler );

    mc_thread_pool_add_task( &ltask , sizeof(mc_thread_task_t) );

    int i = 0 ;

    for(;i < 10000; i++)

    {

        ltask.task_num = i ;

        mc_thread_pool_add_task( &ltask , sizeof(mc_thread_task_t) );

        sleep(1);

    }

    return 0;

} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值