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

Linux中如何实现C线程池

2023-06-09 15:06:00 297人浏览 泡泡鱼
摘要

这篇文章主要为大家展示了“linux中如何实现C线程池”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Linux中如何实现C线程池”这篇文章吧。Linux C线程池三个文件 1 tpoo

这篇文章主要为大家展示了“linux中如何实现C线程池”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Linux中如何实现C线程池”这篇文章吧。

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("%s\n", (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; }

以上是“Linux中如何实现C线程池”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网操作系统频道!

--结束END--

本文标题: Linux中如何实现C线程池

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

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

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

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

下载Word文档
猜你喜欢
  • Linux中如何实现C线程池
    这篇文章主要为大家展示了“Linux中如何实现C线程池”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Linux中如何实现C线程池”这篇文章吧。Linux C线程池三个文件 1 tpoo...
    99+
    2023-06-09
  • Linux如何实现C线程池
    这篇文章主要介绍了Linux如何实现C线程池,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。多线程编程,创建一个线程,指定去完成某一个任务,等待线程的退出。虽然能够满足编程需求...
    99+
    2023-06-28
  • C++线程池实现代码
    前言 这段时间看了《C++并发编程实战》的基础内容,想着利用最近学的知识自己实现一个简单的线程池。 什么是线程池 线程池(thread pool)是一种线程使用模式。线程过多或者频繁...
    99+
    2024-04-02
  • Java中线程池自定义如何实现
    这篇“Java中线程池自定义如何实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Java中线程池自定义如何实现”文章吧。线...
    99+
    2023-07-05
  • GO workPool的线程池如何实现
    今天小编给大家分享一下GO workPool的线程池如何实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Go语言...
    99+
    2023-07-05
  • C#如何通过线程池开启线程
    小编给大家分享一下C#如何通过线程池开启线程,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!通过线程池开启线程 public static&nb...
    99+
    2023-06-17
  • C#中如何实现多线程
    在C#中实现多线程可以使用Thread类或Task类。以下是两种常用的实现方式: 使用Thread类: using System...
    99+
    2024-04-03
    C#
  • SpringBoot实现线程池
    现在由于系统越来越复杂,导致很多接口速度变慢,这时候就会想到可以利用线程池来处理一些耗时并不影响系统的操作。 新建Spring Boot项目 1. ExecutorConfig....
    99+
    2024-04-02
  • python实现线程池
    什么是线程池?     诸如web服务器、数据库服务器、文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务。构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就创建一个新的服务对象,然后在新的服务对象中...
    99+
    2023-01-31
    线程 python
  • 如何实现java简单的线程池
    这篇文章主要讲解了“如何实现java简单的线程池”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何实现java简单的线程池”吧!目录拆分实现流程实现方式拒绝策略阻塞队列线程池和工作线程策略模...
    99+
    2023-06-20
  • C/C++ 原生API实现线程池的方法
    线程池有两个核心的概念,一个是任务队列,一个是工作线程队列。任务队列负责存放主线程需要处理的任务,工作线程队列其实是一个死循环,负责从任务队列中取出和运行任务,可以看成是一个生产者和...
    99+
    2024-04-02
  • Java如何使用线程池实现socket编程
    这篇文章主要讲解了“Java如何使用线程池实现socket编程”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java如何使用线程池实现socket编程”吧!前言以多个客户端和一个服务端的so...
    99+
    2023-06-29
  • linux如何查看线程池状态
    在Linux中,可以使用以下命令来查看线程池的状态:1. 使用top命令:在终端中输入top命令后,可以查看系统中所有正在运行的线程...
    99+
    2023-08-24
    linux
  • Linux C下线程池有什么用
    这篇文章主要介绍了Linux C下线程池有什么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。线程池也是多线程的处理方式。是将“生产者”线程提出任务添加到“任务队列”,然后一...
    99+
    2023-06-15
  • C#实现自定义线程池实例代码
    在项目中如果是web请求时候,IIS会自动分配一个线程来进行处理,如果很多个应用程序共享公用一个IIS的时候,线程分配可能会出现一个问题(当然也是我的需求造成的) 之前在做项目的时候...
    99+
    2024-04-02
  • c++线程池实现的方法是什么
    C++线程池的实现方法可以使用C++中的多线程库,如std::thread和std::mutex等来实现。以下是一个简单的C++线程...
    99+
    2023-10-26
    c++
  • GOworkPool的线程池实现
    Go语言中的线程池是一种用于管理并发执行任务的设计模式。 线程池的主要目的是减少创建和销毁线程的开销,提高系统性能。 在Go语言中,线程池通常使用goroutine和channel来...
    99+
    2023-03-24
    GO workPool线程池 GO workPool
  • jdk线程池的实现
    jdk线程池ThreadPoolExecutor的7个参数 public ThreadPoolExecutor(int corePoolSize, ...
    99+
    2023-05-14
    jdk线程池
  • C++ 多线程编程中线程池的应用
    c++++ 多线程编程中使用线程池的好处包括:1)减少线程创建次数;2)负载均衡;3)避免资源争用。例如,通过使用线程池将图像转换任务分配给线程池,可以提高文件转换应用程序的转换速度。 ...
    99+
    2024-05-14
    多线程 线程池 c++ 标准库
  • 如何用Python实现线程池模型效果
    今天就跟大家聊聊有关如何用Python实现线程池模型效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。本文提供给大家的是用python代码实现一个简单的线程效果源码案例。Python...
    99+
    2023-06-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作