程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C說話完成支撐靜態拓展和燒毀的線程池

C說話完成支撐靜態拓展和燒毀的線程池

編輯:關於C++

C說話完成支撐靜態拓展和燒毀的線程池。本站提示廣大學習愛好者:(C說話完成支撐靜態拓展和燒毀的線程池)文章只能為提供參考,不一定能成為您想要的結果。以下是C說話完成支撐靜態拓展和燒毀的線程池正文


本文實例引見了C 說話完成線程池,支撐靜態拓展和燒毀,分享給年夜家供年夜家參考,詳細內容以下

完成功效

  • 1.初始化指定個數的線程
  • 2.應用鏈表來治理義務隊列
  • 3.支撐拓展靜態線程
  • 4.假如閒置線程過量,靜態燒毀部門線程
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <signal.h>
 

/*線程的義務隊列由,函數和參數構成,義務由鏈表來停止治理*/
typedef struct thread_worker_s{
  void *(*process)(void *arg); //處置函數
  void *arg;          //參數
  struct thread_worker_s *next;
}thread_worker_t;
 
#define bool int
#define true 1
#define false 0
 
/*線程池中各線程狀況描寫*/
#define THREAD_STATE_RUN        0
#define THREAD_STATE_TASK_WAITING   1
#define THREAD_STATE_TASK_PROCESSING  2
#define THREAD_STATE_TASK_FINISHED   3
#define THREAD_STATE_EXIT       4   
 
 
typedef struct thread_info_s{
  pthread_t id;
  int    state; 
  struct thread_info_s *next;
}thread_info_t;
 
static char* thread_state_map[] ={"創立","期待義務","處置中","處置完成","已加入"};
/*線程緊縮的時刻只要 0,1,2,4 狀況的線程可以燒毀*/
 
 
/*線程池治理器*/
#define THREAD_BUSY_PERCENT 0.5  /*線程:義務 = 1:2 值越小,解釋義務多,增長線程*/
#define THREAD_IDLE_PERCENT 2   /*線程:義務 = 2:1 值年夜於1,線程多於義務,燒毀部門線程*/
 
typedef struct thread_pool_s{
  pthread_mutex_t queue_lock ; //隊列互斥鎖,即觸及到隊列修正時須要加鎖
  pthread_cond_t queue_ready; //隊列前提鎖,隊列知足某個前提,觸發期待這個前提的線程持續履行,好比說隊列滿了,隊列空了
 
  thread_worker_t *head   ;    //義務隊列頭指針
  bool    is_destroy   ;    //線程池能否曾經燒毀
  int num;              //線程的個數
  int rnum;         ;    //正在跑的線程
  int knum;         ;    //已殺逝世的線程
  int queue_size       ;    //任務隊列的年夜小 
  thread_info_t *threads   ;    //線程組id,經由過程pthread_join(thread_ids[0],NULL) 來履行線程
  pthread_t   display   ;    //打印線程
  pthread_t   destroy   ;    //按期燒毀線程的線程id
  pthread_t   extend    ;
  float percent       ;    //線程個數於義務的比例 rnum/queue_size
  int  init_num      ;
  pthread_cond_t  extend_ready     ;    //假如要增長線程
}thread_pool_t;
 
/*-------------------------函數聲明----------------------*/
/**
 * 1.初始化互斥變量
 * 2.初始化期待變量
 * 3.創立指定個數的線程線程
 */
thread_pool_t* thread_pool_create(int num);
void *thread_excute_route(void *arg);
 
 
/*調試函數*/
void debug(char *message,int flag){
  if(flag)
    printf("%s\n",message);
}
 
void *display_thread(void *arg);
/**
 * 添加義務包含以下幾個操作
 * 1.將義務添加到隊列末尾
 * 2.告訴期待過程來處置這個義務 pthread_cond_singal();
*/
int thread_pool_add_worker(thread_pool_t *pool,void*(*process)(void *arg),void *arg); //網線程池的隊列中增長一個須要履行的函數,也就是義務
 
/**
 * 燒毀線程池,包含以下幾個部門
 * 1.告訴一切期待的過程 pthread_cond_broadcase
 * 2.期待一切的線程履行完
 * 3.燒毀義務列表
 * 4.釋放鎖,釋放前提
 * 4.燒毀線程池對象
 */
 
 
 
void *thread_pool_is_need_recovery(void *arg);
void *thread_pool_is_need_extend(void *arg);
void thread_pool_destory(thread_pool_t *pool);
 
 
thread_pool_t *thread_pool_create(int num){
  if(num<1){
    return NULL;
  }
  thread_pool_t *p;
  p = (thread_pool_t*)malloc(sizeof(struct thread_pool_s));
  if(p==NULL)
    return NULL;
  p->init_num = num;
  /*初始化互斥變量與前提變量*/
  pthread_mutex_init(&(p->queue_lock),NULL);
  pthread_cond_init(&(p->queue_ready),NULL);
 
  /*設置線程個數*/
  p->num  = num; 
  p->rnum = num;
  p->knum = 0;
 
  p->head = NULL;
  p->queue_size =0; 
  p->is_destroy = false;
 
   
  int i=0;
  thread_info_t *tmp=NULL;
  for(i=0;i<num;i++){
    /*創立線程*/
    tmp= (struct thread_info_s*)malloc(sizeof(struct thread_info_s));
    if(tmp==NULL){
      free(p);
      return NULL;
    }else{
      tmp->next = p->threads;
      p->threads = tmp;
    }
    pthread_create(&(tmp->id),NULL,thread_excute_route,p);
    tmp->state = THREAD_STATE_RUN;
  }
 
  /*顯示*/
  pthread_create(&(p->display),NULL,display_thread,p);
  /*檢測能否須要靜態線程*/
  //pthread_create(&(p->extend),NULL,thread_pool_is_need_extend,p);
  /*靜態燒毀*/
  pthread_create(&(p->destroy),NULL,thread_pool_is_need_recovery,p);
  return p;
}
 
int thread_pool_add_worker(thread_pool_t *pool,void*(*process)(void*arg),void*arg){
  thread_pool_t *p= pool;
  thread_worker_t *worker=NULL,*member=NULL;
  worker = (thread_worker_t*)malloc(sizeof(struct thread_worker_s));
  int incr=0;
  if(worker==NULL){
    return -1;
  }
  worker->process = process;
  worker->arg   = arg;
  worker->next  = NULL;
  thread_pool_is_need_extend(pool);
  pthread_mutex_lock(&(p->queue_lock));
  member = p->head;
  if(member!=NULL){
    while(member->next!=NULL)
      member = member->next;
    member->next = worker;
  }else{
    p->head = worker;
  }
  p->queue_size ++;
  pthread_mutex_unlock(&(p->queue_lock));
  pthread_cond_signal(&(p->queue_ready));
  return 1;
}
 
 
void thread_pool_wait(thread_pool_t *pool){
  thread_info_t *thread;
  int i=0;
  for(i=0;i<pool->num;i++){
    thread = (thread_info_t*)(pool->threads+i);
    thread->state = THREAD_STATE_EXIT;
    pthread_join(thread->id,NULL);
  }
}
void thread_pool_destory(thread_pool_t *pool){
  thread_pool_t  *p   = pool;
  thread_worker_t *member = NULL;
 
  if(p->is_destroy)
    return ;
  p->is_destroy = true;
  pthread_cond_broadcast(&(p->queue_ready));
  thread_pool_wait(pool);
  free(p->threads);
  p->threads = NULL;
  /*燒毀義務列表*/
  while(p->head){
    member = p->head;
    p->head = member->next;
    free(member);
  }
  /*燒毀線程列表*/
  thread_info_t *tmp=NULL;
  while(p->threads){
    tmp = p->threads;
    p->threads = tmp->next;
    free(tmp);
  }
 
  pthread_mutex_destroy(&(p->queue_lock));
  pthread_cond_destroy(&(p->queue_ready));
  return ;
}
/*經由過程線程id,找到對應的線程*/
thread_info_t *get_thread_by_id(thread_pool_t *pool,pthread_t id){
  thread_info_t *thread=NULL;
  thread_info_t *p=pool->threads;
  while(p!=NULL){
    if(p->id==id)
      return p;
    p = p->next;
  }
  return NULL;
}
 
 
/*每一個線程進口函數*/
void *thread_excute_route(void *arg){
  thread_worker_t *worker = NULL;
  thread_info_t  *thread = NULL; 
  thread_pool_t*  p = (thread_pool_t*)arg;
  //printf("thread %lld create success\n",pthread_self());
  while(1){
    pthread_mutex_lock(&(p->queue_lock));
 
    /*獲得以後線程的id*/
    pthread_t pthread_id = pthread_self();
    /*設置以後狀況*/
    thread = get_thread_by_id(p,pthread_id);
 
    /*線程池被燒毀,而且沒有義務了*/
    if(p->is_destroy==true && p->queue_size ==0){
      pthread_mutex_unlock(&(p->queue_lock));
      thread->state = THREAD_STATE_EXIT;
      p->knum ++;
      p->rnum --;
      pthread_exit(NULL);
    }
    if(thread){
      thread->state = THREAD_STATE_TASK_WAITING; /*線程正在期待義務*/
    }
    /*線程池沒有被燒毀,沒有義務到來就一向期待*/
    while(p->queue_size==0 && !p->is_destroy){
      pthread_cond_wait(&(p->queue_ready),&(p->queue_lock));
    }
    p->queue_size--;
    worker = p->head;
    p->head = worker->next;
    pthread_mutex_unlock(&(p->queue_lock));
     
 
    if(thread)
      thread->state = THREAD_STATE_TASK_PROCESSING; /*線程正在履行義務*/
    (*(worker->process))(worker->arg);
    if(thread)
      thread->state = THREAD_STATE_TASK_FINISHED;  /*義務履行完成*/
    free(worker);
    worker = NULL;
  }
}
 
 
 
/*拓展線程*/
void *thread_pool_is_need_extend(void *arg){
  thread_pool_t *p = (thread_pool_t *)arg;
  thread_pool_t *pool = p;
  /*斷定能否須要增長線程,終究目標 線程:義務=1:2*/
  if(p->queue_size>100){
    int incr =0;
    if(((float)p->rnum/p->queue_size) < THREAD_BUSY_PERCENT ){
      incr = (p->queue_size*THREAD_BUSY_PERCENT) - p->rnum; /*盤算須要增長線程個數*/
      int i=0;
      thread_info_t *tmp=NULL;
      thread_pool_t *p = pool;
      pthread_mutex_lock(&pool->queue_lock);
      if(p->queue_size<100){
        pthread_mutex_unlock(&pool->queue_lock);
        return ;
      }
      for(i=0;i<incr;i++){
        /*創立線程*/
        tmp= (struct thread_info_s*)malloc(sizeof(struct thread_info_s));
        if(tmp==NULL){
          continue;
        }else{
          tmp->next = p->threads;
          p->threads = tmp;
        }
        p->num ++;
        p->rnum ++;
        pthread_create(&(tmp->id),NULL,thread_excute_route,p);
        tmp->state = THREAD_STATE_RUN;
      }
      pthread_mutex_unlock(&pool->queue_lock);
    }
  }
  //pthread_cond_signal(&pool->extend_ready);
}
pthread_cond_t sum_ready;
/*恢復初始線程個數*/
void *thread_pool_is_need_recovery(void *arg){
  thread_pool_t *pool = (thread_pool_t *)arg;
  int i=0;
  thread_info_t *tmp = NULL,*prev=NULL,*p1=NULL;
  /*假如沒有義務了,以後線程年夜於初始化的線程個數*/
  while(1){
    i=0;
    if(pool->queue_size==0 && pool->rnum > pool->init_num ){
      sleep(5);
      /*5s秒內照樣這個狀況的話就,燒毀部門線程*/
      if(pool->queue_size==0 && pool->rnum > pool->init_num ){
        pthread_mutex_lock(&pool->queue_lock);
        tmp = pool->threads;
        while((pool->rnum != pool->init_num) && tmp){
          /*找到余暇的線程*/
          if(tmp->state != THREAD_STATE_TASK_PROCESSING){
            i++;
            if(prev)
              prev->next  = tmp->next;
            else
              pool->threads = tmp->next;
            pool->rnum --; /*正在運轉的線程減一*/
            pool->knum ++; /*燒毀的線程加一*/
            kill(tmp->id,SIGKILL); /*燒毀線程*/
            p1 = tmp;
            tmp = tmp->next;
            free(p1);
            continue;
          }
          prev = tmp;
          tmp = tmp->next;
        }
        pthread_mutex_unlock(&pool->queue_lock);
        printf("5s內沒有新義務燒毀部門線程,燒毀了 %d 個線程\n",i);
      }
    }
    sleep(5);
  }
}
 
 
 
/*打印一些信息的*/
void *display_thread(void *arg){
  thread_pool_t *p =(thread_pool_t *)arg;
  thread_info_t *thread = NULL;
  int i=0;
  while(1){
    printf("threads %d,running %d,killed %d\n",p->num,p->rnum,p->knum);  /*線程總數,正在跑的,已燒毀的*/
    thread = p->threads;
    while(thread){
      printf("id=%ld,state=%s\n",thread->id,thread_state_map[thread->state]);
      thread = thread->next;
    }
    sleep(5);
  }
}

願望本文所述對年夜家進修C說話法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved