c语言中的线程池

c语言中没有线程池,但是项目要用到,于是就从网上找了个代码,根据我的情况改了改,大体可以用了。

大概的过程是这样的。
1)初始化线程池,指定最大线程数;
2)将工作线程添加到线程池的等待队列中;
3)创建线程;
4)依次执行线程,等待队列中没有线程的话,线程就会彻底退出了;
5)等待所有线程结束;
6)销毁线程,退出。


PS: 下面的代码有轻微的内存泄露,有没有人可以找出来,我请他吃饭啊。

代码如下:
#include "log_compute.h"
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <assert.h>
using namespace std;
extern sys_conf_t sys_conf;
extern log casmi_log;


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;
     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);
int pool_destroy ();
void pool_init_list (int max_thread_num);
void pool_init_thread();
void pool_wait();

static CThread_pool *pool = NULL;
void
pool_init_list (int max_thread_num)
{
casmi_log.debug("%s(%d)",__FUNCTION__,__LINE__);
     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;

     
}
void
pool_init_thread ()
{
casmi_log.debug("%s(%d)",__FUNCTION__,__LINE__);
pool->threadid =
(pthread_t *) malloc (pool->max_thread_num * sizeof (pthread_t));
int i = 0;
for (i = 0; i < pool->max_thread_num; i++)
{
pthread_create (&(pool->threadid[i]), NULL, thread_routine,
NULL);
casmi_log.debug("%s(%d) created thread 0x%x",__FUNCTION__,__LINE__,pool->threadid[i]);
}
}

int
pool_add_worker (void *(*process) (void *arg), void *arg)
{
casmi_log.debug("%s(%d)",__FUNCTION__,__LINE__);
   
     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 ()
{
casmi_log.debug("%s(%d)",__FUNCTION__,__LINE__);
    if (pool->shutdown)
        return -1;
     pool->shutdown = 1;

   
     pthread_cond_broadcast (&(pool->queue_ready));

   
    int i;
   
     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;
casmi_log.debug("%s(%d)all thread out",__FUNCTION__,__LINE__);
    return 0;
}


void *
thread_routine (void *arg)
{
bool is_start = false;
casmi_log.debug("%s(%d)",__FUNCTION__,__LINE__);
     printf ("starting thread 0x%x\n", pthread_self ());
    while (1)
     {
         pthread_mutex_lock (&(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 ());
if (pool->cur_queue_size <= 0)
{
casmi_log.debug("%s(%d) break 0x%x",__FUNCTION__,__LINE__,pthread_self());
break;
}

       
         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;
if (pool->queue_head == NULL)
{
casmi_log.debug("%s(%d) break 0x%x",__FUNCTION__,__LINE__,pthread_self());
break;
//pthread_exit(NULL);
}
     }
   
     //pthread_exit (NULL);
}

void pool_wait()
{
casmi_log.debug("%s(%d)",__FUNCTION__,__LINE__);
while(pool->cur_queue_size)
{
casmi_log.debug("%s(%d) queue size = %d",__FUNCTION__,__LINE__,pool->cur_queue_size);
sleep(1);
}
for (int i = 0; i < pool->max_thread_num;i++)
{
casmi_log.debug("%s(%d) waiting for 0x%x",__FUNCTION__,__LINE__,pool->threadid[i]);
pthread_join (pool->threadid[i], NULL);
}
casmi_log.debug("%s(%d)all thread out",__FUNCTION__,__LINE__);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值