• 线程池的实现

    • 1:自定义封装的条件变量

      •  //condition.h
        #ifndef _CONDITION_H_
        #define _CONDITION_H_ #include <pthread.h> typedef struct condition
        {
        pthread_mutex_t pmutex;
        pthread_cond_t pcond;
        }condition_t; int condition_init(condition_t *cond);
        int condition_lock(condition_t *cond);
        int condition_unlock(condition_t *cond);
        int condition_wait(condition_t *cond);
        int condition_timewait(condition_t *cond,const struct timespec *abstime);
        int condition_signal(condition_t *cond);
        int condition_broadcast(condition_t *cond);
        int condition_destroy(condition_t *cond); #endif
      •  //condition.c
        #include "condition.h" int condition_init(condition_t *cond)
        {
        int status;
        if((status = pthread_mutex_init(&cond->pmutex,NULL)))
        return status;
        if((status = pthread_cond_init(&cond->pcond,NULL)))
        return status;
        return ;
        } int condition_lock(condition_t *cond)
        {
        return pthread_mutex_lock(&cond->pmutex);
        }
        int condition_unlock(condition_t *cond)
        {
        return pthread_mutex_unlock(&cond->pmutex);
        }
        int condition_wait(condition_t *cond)
        {
        return pthread_cond_wait(&cond->pcond,&cond->pmutex);
        }
        int condition_timewait(condition_t *cond,const struct timespec *abstime)
        {
        return pthread_cond_timedwait(&cond->pcond,&cond->pmutex,abstime);
        }
        int condition_signal(condition_t *cond)
        {
        return pthread_cond_signal(&cond->pcond);
        }
        int condition_broadcast(condition_t *cond)
        {
        return pthread_cond_broadcast(&cond->pcond);
        }
        int condition_destroy(condition_t *cond)
        {
        int status;
        if((status = pthread_mutex_destroy(&cond->pmutex)))
        return status;
        if((status = pthread_cond_destroy(&cond->pcond)))
        return status;
        return ; }
    • 2:线程池逻辑
      •  //threadpool.h
        #include "condition.h" typedef struct task
        {
        void *(*run)(void *arg);
        void *arg;
        struct task *next;
        }task_t; typedef struct threadpool
        {
        condition_t ready;
        task_t *first;
        task_t *last;
        int counter;
        int idle;
        int max_threads;
        int quit;
        }threadpool_t; void threadpool_init(threadpool_t *pool, int threads); void threadpool_add_task(threadpool_t *pool,void *(*run)(void *arg),void *arg); void threadpool_destroy(threadpool_t *pool);
      • //threadpool.c
        #include "threadpool.h"
        #include <string.h>
        #include <errno.h>
        #include <time.h> void *thread_routine(void *arg)
        {
        printf("thread 0x%0x is starting\n",(int)pthread_self());
        struct timespec abstime;
        int timeout; threadpool_t *pool = (threadpool_t*)arg;
        while()
        {
        timeout = ;
        condition_lock(&pool->ready);
        pool->idle++;
        while(pool->first == NULL && pool->quit)
        {
        // condition_wait(&pool->ready);
        clock_gettime(CLOCK_REALTIME, &abstime);
        abstime.tv_sec += ;
        int status = condition_timewait(&pool->ready,&abstime);
        if(status == ETIMEDOUT)
        {
        printf("thread time out\n");
        timeout = ;
        break;
        }
        } pool->idle--;
        if(pool->first != NULL)
        {
        task_t *t = pool->first;
        pool->first = t->next;
          //由于执行下线程工作需要时间,所以先解锁以让其他线程能过获取资源
        condition_unlock(&pool->ready);
        t->run(t->arg);
        free(t);
        condition_lock(&pool->ready);
        } if(pool->quit && pool->first == NULL)
        {
        pool->counter--;
        if(pool->counter == )
        {
        condition_signal(&pool->ready);
        }
        condition_unlock(&pool->ready);
        break;
        } if(timeout && pool->first == NULL)
        {
        pool->counter--;
        condition_unlock(&pool->ready);
        break;
        }
        condition_unlock(&pool->ready);
        } printf("thead 0x%0x is exting\n",(int)pthread_self());
        return NULL;
        }
        void threadpool_init(threadpool_t *pool, int threads)
        {
        // condition_t ready;
        // task_t *first;
        // task_t *last;
        // int counter;
        // int idle;
        // int max_threads;
        // int quit; condition_init(&pool->ready);
        pool->first = NULL;
        pool->last = NULL;
        pool->counter= ;
        pool->idle = ;
        pool->max_threads = threads;
        pool->quit = ;
        } void threadpool_add_task(threadpool_t *pool,void *(*run)(void *arg),void *arg)
        {
        task_t* newtask = malloc(sizeof(task_t));
        newtask->run = run;
        newtask->arg = arg;
        newtask->next = NULL; condition_lock(&pool->ready); //add task to tasklist tail
        if(pool->first ==NULL)
        pool->first = newtask;
        else
        pool->last->next = newtask;
        pool->last = newtask; // if have wait thread,wake to do work
        if(pool->idle > )
        {
        condition_signal(&pool->ready);
        }
        else if(pool->counter < pool->max_threads)
        {
        pthread_t tid;
        pthread_create(&tid, NULL, thread_routine, pool);
        pool->counter++;
        } condition_unlock(&pool->ready);
        } void threadpool_destroy(threadpool_t *pool)
        {
        if(pool->quit)
        {
        return;
        }
        condition_lock(&pool->ready);
        pool->quit = ;
        if(pool->counter > )
        {
        if(pool->idle > )
        {
        condition_broadcast(&pool->ready);
        } while(pool->counter > )
        {
        condition_wait(&pool->ready);
        }
        }
        condition_unlock(&pool->ready);
        condition_destroy(&pool->ready);
        }
    • 3:main

      •  #include "threadpool.h"
        #include <unistd.h>
        #include <stdio.h>
        #include <stdlib.h> void* mytask(void* arg)
        {
        printf("thread 0x%0x is working on task %d\n",(int)pthread_self(),*(int*)arg);
        sleep();
        free(arg);
        return NULL;
        } int main(void)
        {
        threadpool_t pool;
        threadpool_init(&pool,); int i;
        for(i = ; i < ; i++)
        {
        int *arg = (int*)malloc(sizeof(int));
        *arg = i;
        threadpool_add_task(&pool,mytask,arg);
        }
        // sleep(15);
        threadpool_destroy(&pool);
        return ;
        }

Linux线程池的实现的更多相关文章

  1. linux线程池thrmgr源码解析

    linux线程池thrmgr源码解析 1         thrmgr线程池的作用 thrmgr线程池的作用是提高程序的并发处理能力,在多CPU的服务器上运行程序,可以并发执行多个任务. 2      ...

  2. 一个简单的linux线程池(转-wangchenxicool)

    线程池:简单地说,线程池 就是预先创建好一批线程,方便.快速地处理收到的业务.比起传统的到来一个任务,即时创建一个线程来处理,节省了线程的创建和回收的开销,响应更快,效率更高. 在linux中,使用的 ...

  3. 非常精简的Linux线程池实现(一)——使用互斥锁和条件变量

    线程池的含义跟它的名字一样,就是一个由许多线程组成的池子. 有了线程池,在程序中使用多线程变得简单.我们不用再自己去操心线程的创建.撤销.管理问题,有什么要消耗大量CPU时间的任务通通直接扔到线程池里 ...

  4. linux线程池

    typedef struct task_node { void *arg; /* fun arg. */ void *(*fun) (void *); /* the real work of the ...

  5. Linux线程池在server上简单应用

    一.问题描写叙述 如今以C/S架构为例.client向server端发送要查找的数字,server端启动线程中的线程进行对应的查询.将查询结果显示出来. 二.实现方案 1. 整个project以cli ...

  6. Linux下简易线程池

    线程池简介 线程池是可以用来在后台执行多个任务的线程集合. 这使主线程可以自由地异步执行其他任务.线程池通常用于服务器应用程序. 每个传入请求都将分配给线程池中的一个线程,因此可以异步处理请求,而不会 ...

  7. 线程池(Linux实现)

    讨论QQ群:135202158 本文技术参考了sourceforge项目c thread pool,链接:http://sourceforge.net/projects/cthpool/ 线程池如上一 ...

  8. Linux平台下线程池的原理及实现

    转自:http://blog.csdn.net/lmh12506/article/details/7753952 前段时间在github上开了个库,准备实现自己的线程池的,因为换工作的事,一直也没有实 ...

  9. 高效线程池之无锁化实现(Linux C)

    from:http://blog.csdn.net/xhjcehust/article/details/45844901 笔者之前练手写过一个小的线程池版本(已上传至https://github.co ...

随机推荐

  1. VS版本号定义、规则和相关的Visual Studio插件

    软件版本号主要标识了软件的版本,通过其可以了解软件.类库文件的当前版本,使得软件版本控制有所依据. 我们就Windows系统和.NET Framework的编号规则来看,软件版本号的定义结构一般是这样 ...

  2. MySQL联结查询和组合查询

    联结查询 1.关系表 主键:一列或一组列,能够唯一区分表中的每一行,用来表示一个特定的行 外键:为某个表中的一列,包含另一个表的主键,定义量表的关系. 2.创建联结 规定要连接的表和他们如何关联即可 ...

  3. 基于.NET平台常用的框架整理 【转载】

    [转载] http://www.cnblogs.com/hgmyz/p/5313983.html 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个 ...

  4. Hive DDL及DML操作

    一.修改表 增加/删除分区 语法结构 ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec [ LOCATION 'location1' ...

  5. Hadoop HDFS的shell(命令行客户端)操作实例

    HDFS的shell(命令行客户端)操作实例 3.2 常用命令参数介绍 -help 功能:输出这个命令参数手册 -ls                  功能:显示目录信息 示例: hadoop fs ...

  6. PHP-不同Str 拼接方法性能对比

    问题 在PHP中,有多种字符串拼接的方式可供选择,共有: 1 . , .= , sprintf, vprintf, join, implode 那么,那种才是最快的,或者那种才是最适合业务使用的,需要 ...

  7. 字符串CRC校验

    字符串CRC校验 <pre name="code" class="python"><span style="font-family: ...

  8. hmaster 启动后自动关闭

    hbase重装后,hmaster却起不来,多次启动也不行,后来发现原因是在zookeeper中之前注册的hmaster仍然存在,系统中只允许一个hmaster运行.解决方法如下: 进入zk客户端,将h ...

  9. C#发布程序添加其他程序文件

    注:程序发布文件,默认只发布自身程序直接引用的相关文件(A程序). 如果需要添加其他程序(不同的应用程序B)文件,操作方法如下: 第一步:将B程序文件复制到A程序 第二步:将B程序文件右键--> ...

  10. 死磕!Windows下Apache+PHP+phpmyadmin的配置

    环境配置真的很烦很费时间,稍不小心就会出错,这是一个鸡肋体力劳动,耐心和忍耐少不了.这个资料已经非常详细了,其中变量和路径不是百分百吻合但是意思已经很清楚了.剩下的就是耐心的执行和琢磨了. 一.  A ...