iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >Linux C线程池简单实现实例
  • 347
分享到

Linux C线程池简单实现实例

线程实例简单 2022-06-04 21:06:09 347人浏览 独家记忆
摘要

Linux C线程池 三个文件 1 tpool.h typedef struct tpool_work { void (*routine)(void *); void

Linux C线程池

三个文件

1 tpool.h


typedef struct tpool_work { 
  void        (*routine)(void *); 
  void        *arg; 
  struct tpool_work  *next; 
} tpool_work_t; 
 
typedef struct tpool { 
   
  int         num_threads; 
  int         max_queue_size; 
   
  pthread_t      *tpid; 
  tpool_work_t    *queue; 
  int         front, rear; 
   
  int         queue_closed;   
   
  int         shutdown;     
   
  pthread_mutex_t   queue_lock; 
  pthread_cond_t   queue_has_task; 
  pthread_cond_t   queue_has_space; 
  pthread_cond_t   queue_empty; 
} *tpool_t; 
 
void tpool_init(tpool_t *tpoolp,int num_threads, int max_queue_size); 
 
int tpool_add_work(tpool_t tpool,void(*routine)(void *), void *arg); 
 
int tpool_destroy(tpool_t tpool,int finish); 

2 tpool.c


#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <string.h> 
#include <pthread.h> 
#include "tpool.h" 
 
#define DEBUG 
 
#if defined(DEBUG) 
#define debug(...) do {  
  flockfile(stdout);  
  printf("###%p.%s: ", (void *)pthread_self(), __func__);  
  printf(__VA_ARGS__);  
  putchar('n');  
  fflush(stdout);  
  funlockfile(stdout);  
} while (0) 
#else 
#define debug(...) 
#endif 
 
void *tpool_thread(void *); 
 
void tpool_init(tpool_t *tpoolp, int num_worker_threads, int max_queue_size) 
{ 
  int i; 
  tpool_t pool; 
 
  pool = (tpool_t)malloc(sizeof(struct tpool)); 
  if (pool == NULL) { 
    perror("malloc"); 
    exit(0); 
  } 
 
  pool->num_threads = 0; 
  pool->max_queue_size = max_queue_size + 1; 
  pool->num_threads = num_worker_threads; 
  pool->tpid = NULL; 
  pool->front = 0; 
  pool->rear = 0; 
  pool->queue_closed = 0; 
  pool->shutdown = 0; 
 
  if (pthread_mutex_init(&pool->queue_lock, NULL) == -1) { 
    perror("pthread_mutex_init"); 
    free(pool); 
    exit(0); 
  } 
  if (pthread_cond_init(&pool->queue_has_space, NULL) == -1) { 
    perror("pthread_mutex_init"); 
    free(pool); 
    exit(0); 
  } 
  if (pthread_cond_init(&pool->queue_has_task, NULL) == -1) { 
    perror("pthread_mutex_init"); 
    free(pool); 
    exit(0); 
  } 
  if (pthread_cond_init(&pool->queue_empty, NULL) == -1) { 
    perror("pthread_mutex_init"); 
    free(pool); 
    exit(0); 
  } 
 
  if ((pool->queue = malloc(sizeof(struct tpool_work) *  
          pool->max_queue_size)) == NULL) { 
    perror("malloc"); 
    free(pool); 
    exit(0); 
  } 
 
  if ((pool->tpid = malloc(sizeof(pthread_t) * num_worker_threads)) == NULL) { 
    perror("malloc"); 
    free(pool); 
    free(pool->queue); 
    exit(0); 
  } 
 
  for (i = 0; i < num_worker_threads; i++) { 
    if (pthread_create(&pool->tpid[i], NULL, tpool_thread,  
          (void *)pool) != 0) { 
      perror("pthread_create"); 
      exit(0); 
    } 
  } 
 
  *tpoolp = pool; 
} 
 
 
int empty(tpool_t pool) 
{ 
  return pool->front == pool->rear; 
} 
 
int full(tpool_t pool) 
{ 
  return ((pool->rear + 1) % pool->max_queue_size == pool->front); 
} 
 
int size(tpool_t pool) 
{ 
  return (pool->rear + pool->max_queue_size - 
        pool->front) % pool->max_queue_size; 
} 
 
int tpool_add_work(tpool_t tpool, void(*routine)(void *), void *arg) 
{ 
  tpool_work_t *temp; 
 
  pthread_mutex_lock(&tpool->queue_lock); 
 
  while (full(tpool) && !tpool->shutdown && !tpool->queue_closed) { 
    pthread_cond_wait(&tpool->queue_has_space, &tpool->queue_lock); 
  } 
 
  if (tpool->shutdown || tpool->queue_closed) { 
    pthread_mutex_unlock(&tpool->queue_lock); 
    return -1; 
  } 
 
  int is_empty = empty(tpool); 
 
  temp = tpool->queue + tpool->rear; 
  temp->routine = routine; 
  temp->arg = arg; 
  tpool->rear = (tpool->rear + 1) % tpool->max_queue_size; 
 
  if (is_empty) { 
    debug("signal has task"); 
    pthread_cond_broadcast(&tpool->queue_has_task); 
  } 
 
  pthread_mutex_unlock(&tpool->queue_lock);   
 
  return 0; 
} 
 
void *tpool_thread(void *arg) 
{ 
  tpool_t pool = (tpool_t)(arg); 
  tpool_work_t *work; 
 
  for (;;) { 
    pthread_mutex_lock(&pool->queue_lock); 
 
    while (empty(pool) && !pool->shutdown) { 
      debug("I'm sleep"); 
      pthread_cond_wait(&pool->queue_has_task, &pool->queue_lock); 
    } 
    debug("I'm awake"); 
 
    if (pool->shutdown == 1) { 
      debug("exit"); 
      pthread_mutex_unlock(&pool->queue_lock); 
      pthread_exit(NULL); 
    } 
 
    int is_full = full(pool); 
    work = pool->queue + pool->front; 
    pool->front = (pool->front + 1) % pool->max_queue_size; 
 
    if (is_full) { 
      pthread_cond_broadcast(&pool->queue_has_space); 
    } 
 
    if (empty(pool)) { 
      pthread_cond_signal(&pool->queue_empty); 
    } 
 
    pthread_mutex_unlock(&pool->queue_lock);   
 
    (*(work->routine))(work->arg); 
  } 
} 
 
int tpool_destroy(tpool_t tpool, int finish) 
{ 
  int   i; 
 
  pthread_mutex_lock(&tpool->queue_lock); 
 
  tpool->queue_closed = 1; 
 
  if (finish == 1) { 
    debug("wait all work done"); 
    while (!empty(tpool)) { 
      pthread_cond_wait(&tpool->queue_empty, &tpool->queue_lock); 
    } 
  } 
  tpool->shutdown = 1; 
 
  pthread_mutex_unlock(&tpool->queue_lock); 
 
  pthread_cond_broadcast(&tpool->queue_has_task); 
 
  debug("wait worker thread exit"); 
  for (i = 0; i < tpool->num_threads; i++) { 
    pthread_join(tpool->tpid[i], NULL); 
  } 
 
  debug("free thread pool"); 
  free(tpool->tpid); 
  free(tpool->queue); 
  free(tpool); 
} 
 

3 tpooltest.c


#include <stdio.h> 
#include <pthread.h> 
#include "tpool.h" 
 
char *str[]={"string 0", "string 1", "string 2",  
        "string 3", "string 4", "string 5"}; 
 
void job(void * jobstr) 
{ 
  long i, x; 
 
  for (i = 0; i < 100000000; i++) { 
    x = x +i; 
  } 
  printf("%sn", (char *)jobstr); 
} 
 
int main(void) 
{ 
  int i;  
  tpool_t test_pool; 
 
  tpool_init(&test_pool, 8, 20); 
 
  for ( i = 0; i < 5; i++) { 
    tpool_add_work(test_pool, job, str[i]); 
  } 
 
  tpool_destroy(test_pool, 1); 
 
  return 0; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

--结束END--

本文标题: Linux C线程池简单实现实例

本文链接: https://www.lsjlt.com/news/18701.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java简单实现线程池
    本文实例为大家分享了Java简单实现线程池的具体代码,供大家参考,具体内容如下 一、线程池 线程池是一种缓冲提高效率的技术。 相当于一个池子,里面存放大量已经创建好的线程,当有一个任...
    99+
    2024-04-02
  • 实现java简单的线程池
    目录拆分实现流程实现方式1.拒绝策略2.阻塞队列3.线程池和工作线程策略模式对比JDK的线程池线程池的状态转化总结拆分实现流程 请看下面这张图 首先我们得对线程池进行一个功能拆分 ...
    99+
    2024-04-02
  • C++实现一个简单的线程池的示例代码
    目录一、设计二、参数选择三、类设计一、设计 线程池应该包括 保存线程的容器,保存任务的容器。为了能保证避免线程对任务的竞态获取,需要对任务队列进行加锁。为了使得工作线程感知任务的到来...
    99+
    2024-04-02
  • C++怎么实现一个简单的线程池
    本文小编为大家详细介绍“C++怎么实现一个简单的线程池”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++怎么实现一个简单的线程池”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。一、设计线程池应该包括保存线程的容...
    99+
    2023-06-30
  • springboot线程池监控的简单实现
    目录背景代码代码类结构线程池扩展类线程工具类线程bean类线程池实现类线程池监控接口类运行结果背景 在我们实际项目开发中,常常会为不同的优先级的任务设置相对应的线程池。一般我们只关注...
    99+
    2024-04-02
  • 如何实现java简单的线程池
    这篇文章主要讲解了“如何实现java简单的线程池”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何实现java简单的线程池”吧!目录拆分实现流程实现方式拒绝策略阻塞队列线程池和工作线程策略模...
    99+
    2023-06-20
  • Linux如何实现C线程池
    这篇文章主要介绍了Linux如何实现C线程池,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。多线程编程,创建一个线程,指定去完成某一个任务,等待线程的退出。虽然能够满足编程需求...
    99+
    2023-06-28
  • C++单例模式实现线程池的示例代码
    C语言单例模式实现线程池。 该代码中,使用了单例模式来创建线程池对象,保证了整个程序中只有一个线程池对象。 线程池中包含了任务队列、工作线程数组、互斥锁、条件变量等成员,通过这些成员...
    99+
    2023-05-16
    C++单例模式实现线程池 C++单例模式 线程池 C++ 线程池 C++ 单例模式
  • Go简单实现协程池的实现示例
    目录MPG模型通道的特性首先就是进程、线程、协程讲解老三样。 进程: 本质上是一个独立执行的程序,进程是操作系统进行资源分配和调度的基本概念,操作系统进行资源分配和调度的一...
    99+
    2024-04-02
  • C#实现多线程编程的简单案例
    目录一、使用线程的理由二、基本知识三、线程的使用四、线程池五、Task类六、委托异步执行一、使用线程的理由 1、可以使用线程将代码同其他代码隔离,提高应用程序的可靠性。2、可以使用线...
    99+
    2024-04-02
  • 用Python实现一个简单的线程池
    线程池的概念是什么?在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源。在Java中更是 如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收。所以提高服务程序效率的一个手段就是尽可能...
    99+
    2023-01-31
    线程 简单 Python
  • Linux中如何实现C线程池
    这篇文章主要为大家展示了“Linux中如何实现C线程池”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Linux中如何实现C线程池”这篇文章吧。Linux C线程池三个文件 1 tpoo...
    99+
    2023-06-09
  • C++内存池的简单实现
    目录一、内存池基础知识1、什么是内存池1.1 池化技术1.2 内存池2、内存池的作用2.1 效率问题2.2 内存碎片3、内存池技术的演进二、简易内存池原理1、整体设计1.1 内存池结...
    99+
    2024-04-02
  • python 进程池pool简单实例
    进程池:      在利用Python进行系统管理的时候,特别是同时操作多个文件目录,或者远程控制多台主机,并行操作可以节约大量的时间。当被操作对象数目不大时,可以直接利用multiprocessing中的Process动态成生多个进程,十...
    99+
    2023-01-31
    实例 进程 简单
  • Java线程池的简单使用方法实例教程
    目录线程池使用场景? Java线程池使用总结线程池使用场景? java中经常需要用到多线程来处理一些业务,我们非常不建议单纯使用继承Thread或者实现Runnable接口...
    99+
    2024-04-02
  • C#实现自定义线程池实例代码
    在项目中如果是web请求时候,IIS会自动分配一个线程来进行处理,如果很多个应用程序共享公用一个IIS的时候,线程分配可能会出现一个问题(当然也是我的需求造成的) 之前在做项目的时候...
    99+
    2024-04-02
  • java实现手写一个简单版的线程池
    有些人可能对线程池比较陌生,并且更不熟悉线程池的工作原理。所以他们在使用线程的时候,多数情况下都是new Thread来实现多线程。但是,往往良好的多线程设计大多都是使用线程池来实现...
    99+
    2024-04-02
  • python进程池的简单实现
    目录创建进程池向进程池提交任务并行执行多个任务关闭进程池等待任务执行完毕Python进程池是Python标准库中multiprocessing模块提供的一种用于管理进程的方式。它可以...
    99+
    2023-03-13
    python进程池
  • C++线程池实现代码
    前言 这段时间看了《C++并发编程实战》的基础内容,想着利用最近学的知识自己实现一个简单的线程池。 什么是线程池 线程池(thread pool)是一种线程使用模式。线程过多或者频繁...
    99+
    2024-04-02
  • Qt5.9实现简单的多线程实例(类QThread)
    Qt开启多线程,主要用到类QThread。有两种方法,第一种用一个类继承QThread,然后重新改写虚函数run()。当要开启新线程时,只需要实例该类,然后调用函数start(),就...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作