計算機中常用的池(池式結構)

内存池

數據庫連接池

請求池

消息隊列(消息池)

對象池

緩衝區

綫程池解決問題

異步解耦的作用,如讀寫磁盤,檢測IO

綫程池三個部分

1 多個綫程s

2 多個任務

3 管理組件

綫程池部分代碼

#include<stdio.h>
#include <pthread.h>struct NWORKER
{pthread_t thread;struct NWORKER* pool;int terminate;struct NWORKER* prev;struct NWORKER* next;
};struct NJOB
{void (*func)(struct NJOB* job);void* user_data;struct NJOB * prev;struct NJOB * next;};struct NMANAGER
{struct NWORKER* workers;struct NJOB* jobs;pthread_cond_t jobs_cond;pthread_mutex_jobs_mutex;//任務隊列是臨界資源,綫程不能同時進行訪問
};//使用頭插法增加任務,
#define LL_ADD(item,list) do{
item->prev = NULL;
item->next = list;
list->prev = item;
list = item;
}while (0);#define LL_REMOVE(item,list) do{
if (item - < prev != NULL)item->prev->next = item->next;
if(item->prev!=NULL)item->next->prev = item->prev;
if (list == item)list = item->next;
item->prev = item->next = NULL;
}while (0);//靜態函數  只在本文件有效
static void* nThreadCallBack(void *arg)
{struct NWORKER* worker = (struct NWORKER) arg;while (1){//條件等待前加鎖pthread_mutex_lock(&worker->pool->jobs_mutex);while (worker->pool->jobs == NULL){//if (worker->terminate) break;pthread_cond_wait(worker->pool->jobs_cond, worker->pool->jobs_mutex);}if (worker->terminate){pthread_mutex_unlock(&worker->pool->jobs_mutex);}struct NJOB* job = worker->pool->jobs;LL_REMOVE(job,worker->pool->jobs);pthread_mutex_unlock(&worker->pool->jobs_mutex);job->func(job->user_data);}free(worker);pthread_exit(NULL);
}typedef struct NWORKER nThreadPool;
//綫程池初始化  numworkers 綫程數量
int nThreadPoolCreate(nThreadPool *pool, int numworkers)
{if (numworkers < 1) numworkers = 1;if (pool == NULL) return -1;memset(pool,0,sizeof(nThreadPool));//内存拷貝pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;memcpy(&pool->jobs_cond, &blank_cond,sizeof(pthread_cond_t));pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;memcpy(&pool->jobs_mutex,&blank_mutex,sizeof(pthread_mutex_t)));int i = 0;for (int i = 0; i < numworkers; i++){struct NWORKER* worker = (struct NWORKER*) malloc(sizeof(struct NWORKER));if (worker == NULL){perror(*mallloc);return -2;}//一定要初始化memset(worker, 0, sizeof(struct WORKER));worker->pool = pool;int ret = pthread_create(worker->thread, NULL,nThreadCallBack,worker);if (ret) {perror("pthread_create");free("worker");return -3;}//每創建一個綫程 就把它加入綫程池LL_ADD(worker, pool->workers);}return pool;
}int nThreadPoolDestroy(nThreadPool* pool)
{struct NWORKER* worker = NULL;for (worker* pool->worker != NULL; worker = worker->next){worker->terminate = 1;}pthread_mutex_lock(&pool->jobs_mutex);pthread_cond_broadcast(pool->jobs_cond);pthread_mutex_unlock(&pool->jobs_mutex);
}void nThreadPoolPush(nThreadPool pool, struct NJOB *job)
{pthread_mutex_lock(&pool->jobs_mutex);LL_ADD(job, pool->jobs);pthread_cond_signal(&pool->jobs_cond);//喚醒其中一個綫程為其服務pthread_mutex_unlock(&pool->jobs_mutex);
}

綫程池 部分代碼實現 筆記相关推荐

  1. latent_3d_points代碼付現:Learning Representations and Generative Models For 3D Point Clouds

    第一步:geihub下載代碼 latent_3d_points:https://github.com/optas/latent_3d_points 第二:安裝環境 Ubuntu 16.04 Pytho ...

  2. php 服務器連接,cocos2d-x網絡編程 連接php服務器筆記4

    VS工程部分----網絡編程 本節會把最終實現代碼和資源放在文章最未提供各位下載學習. 本節我們開始重頭戲聯網功能的開發,我用的是cocos2d-x綁定的curl庫,這個curl據說很火,雖然我本人了 ...

  3. Real-time hatching報告+實現代碼和效果

    1.論文解讀 論文標題為實時的畫影線,實時的畫影線的主要作用可以用於產生素描畫的效果.該論文屬於計算機圖形學中的圖像處理技術和紋理技術. 根據摘要,論文可以被分為四個部分.該論文通過分析已有技術中的問 ...

  4. C#適應練習:幾種常見設計模式的實現

    一.單例及原型模式 單例:即使用一個固定對象的對象進行操作,實現起來很簡單 using System; using System.Collections.Generic; using System.L ...

  5. ASP + Serv-u 實現FTP的代碼

    ASP + Serv-u 實現FTP的代碼 發佈者:[飛翔] 瀏覽:[ ] 評論:[0]  <!--#include file="md5.asp"--> <% ' ...

  6. 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程

    目錄: 開玩樹莓派(一):安裝Raspbian系統 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程 開玩樹莓派(三):Python編程 開玩樹莓派(四):GPIO控制和遠程 ...

  7. linux 有名管道pipe,linux 用無名管道pipe和有名管道fifo實現線程間通信

    1.pipe 用與實現同一個進程下不同線程間的通信(跟IPC進程間通信中的具有血緣關系的進程通信實現方式一樣) #include #include #include #include #include ...

  8. 应用调优常用技巧-線程池

    应用调优常用技巧-線程池 应用调优常用技巧 - 线程池 線程池的好處 核心API-操作類 核心API-監控類 2-2 线程池BlockingQueue详解.选择与调优 調優技巧 2-3 线程池Sche ...

  9. mysql 开启 thread pool_MySQL線程池(THREAD POOL)的處理

    背景介紹 MySQL常用(目前線上使用)的線程調度方式是one-thread-per-connection(每連接一個線程),server為每一個連接創建一個線程來服務,連接斷開后,這個線程進入thr ...

最新文章

  1. android studio导入aar包,AndroidStudio导入本地aar文件
  2. 【python】Series和DataFrame的简单介绍
  3. python获取命令行参数的方法
  4. hdc和hwnd的区别
  5. CTSCAPIO被教做人记
  6. win10系统崩溃怎么修复_系统崩溃怎么重装系统图文教程
  7. 阿里AI智能音箱现在有了视觉能力,跟人交互时表情丰富
  8. 服务器(Windows系统)自建filebrowser网盘服务器超详细教程
  9. head标签中到底可以放什么?
  10. Python如何连mysql数据库教程
  11. 郝斌PHP视频教程,郝斌数据结构自学视频_IT教程网
  12. 「R shiny基础」使用shinyapp分享你的Shiny应用
  13. 高博课程编程作业之计算小萝卜的坐标
  14. ubuntu16.04+Tesla P100+cuda+anaconda+cudnn+tensorflow:从0开始安装
  15. printf的计算和输出顺序
  16. ASO优化优缺点各是什么?带你学会常见的优化手段
  17. java一次能迈一级或两级台阶_有个人想上一个n级的台阶,每次只能迈1级或者迈2级台阶,问:这个人有多少种方法可以把台阶走完?...
  18. 多个精美的导航样式web2.0源码
  19. JavaScript函数,作用域以及闭包
  20. 给在线发布的cab文件进行数字签证

热门文章

  1. 小白福利-手把手教你如何重新安装你的系统
  2. 小程序-实现列表- 搜索功能的实现(6)
  3. 写一个PE的壳_Part 5:PE格式修复+lief源码修改
  4. 行业内参:2019年支付监管框架将有大调整 线上线下牌照合并重划
  5. 零基础学SQL(二、MYSQL数据类型)
  6. 散射回波仿真Matlab,基于散射中心模型的ISAR回波仿真方法
  7. ChatGPT聊天机器人如何发图片????
  8. C函数参数中的三个点
  9. vue 导出word文档(包括图片)
  10. 用Midjourney画个美女,AI绘画也太强大了!!! - 第8篇