zoukankan      html  css  js  c++  java
  • 线程池的简单实现

    /*pthreadpool.h*/

    #ifndef PTHREADPOOL
    2 #define PTHREADPOOL
    3 #include<pthread.h>
    4
    5 struct task_node{
    6
    7 void *(*run)(void *arg);//一个函数指针
    8 void *arg;//函数指针的参数
    9 struct task_node *next;
    10 };
    11
    12 typedef struct task_node task_node;
    13
    14 struct pthreadpool{
    15 task_node *head;//任务队列的头指针
    16 task_node *top;//任务队列的尾指针
    17
    18 int max_pthread;//线程池创建线程的最大数
    19
    20 int pthread_num;//线程的数目
    21 int pthread_wait_num;//等待线程的数目
    22 int pthread_work_num;//工作线程的数目
    23
    24 pthread_mutex_t mutex;// 用于保护任务队列的互斥锁
    25 pthread_cond_t cond;// 一个条件变量
    26 };
    27 typedef struct pthreadpool pthreadpool;
    28
    29 //线程池的初始化
    30 void pthreadpool_init(pthreadpool *pool,int maxthread);
    31 //向线程池添加任务
    32 void pthreadpool_addtask(pthreadpool *pool,void*(*run)(void *arg),voi d *arg);
    33 //销毁线程池
    34 void pthreadpool_destory(pthreadpool * pool);
    35
    36

    /*pthreadpool.c*/

    1 #include"pthreadpool.h"
    2 #include<stdlib.h>
    3 #include<stdio.h>
    4 void *mythread(void *arg)
    5 {
    6 pthreadpool *pool=(pthreadpool *)arg;
    7 while(1)
    8 {
    9 printf("%uwait.....\n",pthread_self());
    10 pool->pthread_wait_num++;
    13 {
    14 pthread_cond_wait(&pool->cond,&pool->mutex);
    15
    18
    19 //取得任
    20 task_node *head=pool->head;
    21 pool->head=pool->head->next;
    23 pthread_mutex_unlock(&pool->mutex);
    24 printf("%uwork....\n",pthread_self());
    25 head->run(head->arg);
    26 free(head);
    27
    28
    29 }
    30 }
    31
    32
    33 void pthreadpool_init(pthreadpool *pool,int maxthread)
    34 {
    35 pool->head=NULL;
    36 pool->top=NULL;
    37 pool->max_pthread=maxthread;
    40 pool->pthread_work_num=0;
    41
    42 pthread_mutex_init(&pool->mutex,NULL);
    45
    46
    48 {
    49 //创建一个任务
    50 task_node *node=malloc(sizeof(task_node));
    1 #include"pthreadpool.h"
    2 #include<stdlib.h>
    3 #include<stdio.h>
    4 void *mythread(void *arg)
    5 {
    6 pthreadpool *pool=(pthreadpool *)arg;
    1 #include"pthreadpool.h"
    2 #include<stdlib.h>
    3 #include<stdio.h>
    4 void *mythread(void *arg)
    5 {
    6 pthreadpool *pool=(pthreadpool *)arg;
    7 while(1)
    8 {
    9 printf("%uwait.....\n",pthread_self());
    10 pool->pthread_wait_num++;
    11 pthread_mutex_lock(&pool->mutex);
    12 while( pool->head==NULL)
    13 {
    14 pthread_cond_wait(&pool->cond,&pool->mutex);
    15
    16 }
    17 pool->pthread_wait_num--;
    18
    19 //取得任
    20 task_node *head=pool->head;
    21 pool->head=pool->head->next;
    22 //这里应该解锁因为任务一伙的防止在执行任务的时候其他线程不能获得任务
    23 pthread_mutex_unlock(&pool->mutex);
    24 printf("%uwork....\n",pthread_self());
    25 head->run(head->arg);
    26 free(head);
    27 sleep(1);
    28
    29
    30 }
    31 }
    32
    33
    34 void pthreadpool_init(pthreadpool *pool,int maxthread)
    35 {
    36 pool->head=NULL;
    37 pool->top=NULL;
    38 pool->max_pthread=maxthread;
    1 #include"pthreadpool.h"
    2 #include<stdlib.h>
    3 #include<stdio.h>
    4 void *mythread(void *arg)
    5 {
    6 pthreadpool *pool=(pthreadpool *)arg;
    7 while(1)
    8 {
    9 printf("%uwait.....\n",pthread_self());
    10 pool->pthread_wait_num++;
    11 pthread_mutex_lock(&pool->mutex);
    12 while( pool->head==NULL)
    13 {
    14 pthread_cond_wait(&pool->cond,&pool->mutex);
    15
    16 }
    17 pool->pthread_wait_num--;
    18
    19 //取得任
    20 task_node *head=pool->head;
    21 pool->head=pool->head->next;
    22 //这里应该解锁因为任务一伙的防止在执行任务的时候其他线程不能获得任务
    23 pthread_mutex_unlock(&pool->mutex);
    24 printf("%uwork....\n",pthread_self());
    25 head->run(head->arg);
    26 free(head);
    27 sleep(1);
    28
    29
    30 }
    31 }
    32
    33
    34 void pthreadpool_init(pthreadpool *pool,int maxthread)
    26 free(head);
    27 sleep(1);
    28
    29
    30 }
    31 }
    32
    33
    34 void pthreadpool_init(pthreadpool *pool,int maxthread)
    35 {
    36 pool->head=NULL;
    37 pool->top=NULL;
    38 pool->max_pthread=maxthread;
    39 pool->pthread_num=0;
    40 pool->pthread_wait_num=0;
    41 pool->pthread_work_num=0;
    42
    43 pthread_mutex_init(&pool->mutex,NULL);
    44 pthread_cond_init(&pool->cond,NULL);
    45 }
    46
    47
    48 void pthreadpool_addtask(pthreadpool *pool,void*(*run)(void *arg),void *arg)
    49 {
    50 //创建一个任务
    51 task_node *node=malloc(sizeof(task_node));
    52 node->run=run;
    53 node->arg=arg;
    54 node->next=NULL;
    55
    56 //添加到任务队列
    57 pthread_mutex_lock(&pool->mutex);
    58
    59 if(pool->head==NULL)
    60 {
    61 pool->head=node;
    62 pool->top=node;
    63 }else{
    64
    65 pool->top->next=node;
    66 pool->top=node;
    67 }
    68
    69 //添加完任务判断是否有等待线程有唤醒没有创建
    70 if(pool->pthread_wait_num>0)
    71 {
    72 pthread_cond_signal(&pool->cond);
    73 }
    74
    75 if(pool->pthread_wait_num==0&&pool->pthread_num<pool->max_pthread)
    76 {
    77 printf("cread pthread...\n");
    78 pthread_t pid;
    79 pthread_create(&pid,NULL,mythread,(void *)pool);
    80 pool->pthread_num++;
    81 }
    82
    83 pthread_mutex_unlock(&pool->mutex);
    84
    85
    86 }
    87
    88 void pthreadpool_destory(pthreadpool * pool)
    89 {
    90
    91
    92
    93
    94
    95 }
    测试代码

    1 #include<stdio.h>
    2 #include"pthreadpool.h"
    3 #include<stdlib.h>
    4 void * run(void *arg)
    5 {
    6 int num=*(int *)arg;
    7 printf("task num %d:",num);
    8 printf("hellow world\n");
    9 return NULL;
    10 }
    11 int main()
    12 {
    13 int i=0;
    14 pthreadpool pool;
    15 pthreadpool_init(&pool,10);
    16 for(int i=0;i<10;i++)
    17 {
    18 int *arg=malloc(sizeof(int));
    19 *arg=i;
    20 pthreadpool_addtask(&pool,run,arg);
    21 sleep(5);
    22 }
    23
    24
    25 while(1);
    26
    27
    28
    29
    30
    31
    32
    33
    34 }

    线程池的要点:

        线程数目的确定:这个要看具体的任务类型

                           任务类型分为:1 .io密集型任务  这种类型我们可以开的线程数多于cpu的数目

                                 因为种类型可能回不断地阻塞到io这块所以对cpu的不会频繁;

                                  2. 计算密集型任务 这种类型的任务很吃cpu一般不会阻塞到其他地方所以最好线程数等于cpu数目;

                                    因为如果开的线程远大于cpu数这样频繁的切换线程会非常的耗时反而没有达到高效率

                  

        可以随着任务的增加自己动态的开辟线程;

        也可以动态的销毁线程:实现方案我们可以在条件变量设置一个超时等待如果超时说明任务无或者较少则进行销毁;

        

  • 相关阅读:
    WINCE6.0+S3C6410睡眠和唤醒的实现
    WINCE6.0+S3C6410的触摸屏驱动
    S3C6410的Bootloader的两个阶段BL1和BL2编译相关学习
    amix vim vimrc 3.6 [_vimrc x64 vim (WorkPlace)]配置
    异常的开销
    A C# Reading List by Eric Lippert (ZZ)
    SQL SERVER 2008中定时备份数据库任务的创建与删除
    ASP.NET26个常用性能优化方法
    如何使用四个语句来提高 SQL Server 的伸缩性
    Cookies揭秘 [Asp.Net, Javascript]
  • 原文地址:https://www.cnblogs.com/jzlzn/p/9996418.html
Copyright © 2011-2022 走看看