c语言中的线程池

来源:互联网 发布:vb精简版 编辑:程序博客网 时间:2024/06/06 02:52

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__);
}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩子写作业爱磨蹭怎么办 孩子在学校不写作业怎么办 鳗鱼刺卡在喉咙怎么办 被小鱼刺卡住了怎么办 喉咙上卡了鱼刺怎么办 跟团出去受伤了怎么办 平安易宝冻结了怎么办 车的保险到期没有交怎么办 人保外地险出险怎么办 婚姻经营不下去了怎么办 他不爱我,我该怎么办 没有我你怎么办钢琴版 没有你怎么办严爵歌词 没有羊毛戳针该怎么办 没有我你该怎么办歌词 雌激素低子宫内膜薄怎么办 没有我你怎么办的句子 没有我你怎么办百度云 何润东没有我你怎么办 想你了我该怎么办 没有你日子我该怎么办 没有我你怎么办mp3下载 如果没有你,我该怎么办 我该怎么办韩剧插曲 没有我你怎么办微盘 歌曲没有你我该怎么办 uc看小说收费了怎么办 发财树黄叶掉叶怎么办 5个月猫咪嘴巴臭怎么办 灿盛入伍柳岩怎么办 手机电用得快怎么办 身份证过期7个月怎么办 耳洞总是有臭味怎么办 口琴24孔吹不准怎么办 狗狗反胃吐白沫怎么办 2岁宝宝牙齿被腐蚀怎么办 2岁宝宝乳牙腐蚀怎么办 1岁宝宝门牙腐蚀怎么办 3岁宝宝有蛀牙怎么办 3岁宝宝乳牙腐蚀怎么办 三岁宝宝烂牙怎么办