Linux C线程池简单实现实例
发布时间 - 2026-01-11 02:11:17 点击率:次Linux C线程池

三个文件
1 tpool.h
typedef struct tpool_work {
void (*routine)(void *);
void *arg;
struct tpool_work *next;
} tpool_work_t;
typedef struct tpool {
/* pool characteristics */
int num_threads;
int max_queue_size;
/* pool state */
pthread_t *tpid;
tpool_work_t *queue;
int front, rear;
/* 剩下的任务可以做完, 但不能再加新的任务 */
int queue_closed;
/* 剩下的任务都不做了, 直接关闭 */
int shutdown;
/* pool synchronization */
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线程池
# 线程池的实例
# Linux C中多线程与volatile变量
# Linux线程退出方式总结(推荐)
# linux线程的取消(终止)方法
# 浅谈linux线程切换问题
# linux线程切换和进程切换的方法
# linux多线程编程(五)
# linux多线程编程(四)
# Linux下的多线程编程(三)
# Linux多线程编程(一)
# 希望能
# 不做
# 谢谢大家
# 做完
# 再加
# tpool_t
# queue_empty
# queue_has_space
# tpool_add_work
# tpoolp
# tpool_init
# queue_has_task
# synchronization
# shutdown
# strong
# pthread_cond_t
# queue_lock
# pthread_mutex_t
# sys
# unistd
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
EditPlus中的正则表达式 实战(4)
网站建设要注意的标准 促进网站用户好感度!
Python图片处理进阶教程_Pillow滤镜与图像增强
如何在服务器上配置二级域名建站?
🚀拖拽式CMS建站能否实现高效与个性化并存?
如何快速搭建FTP站点实现文件共享?
矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?
标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南
1688铺货到淘宝怎么操作 1688一键铺货到自己店铺详细步骤
如何在七牛云存储上搭建网站并设置自定义域名?
Win11任务栏卡死怎么办 Windows11任务栏无反应解决方法【教程】
python中快速进行多个字符替换的方法小结
Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能
Laravel中间件如何使用_Laravel自定义中间件实现权限控制
html文件怎么打开证书错误_https协议的html打开提示不安全【指南】
如何在阿里云ECS服务器部署织梦CMS网站?
Laravel怎么设置路由分组Prefix_Laravel多级路由嵌套与命名空间隔离【步骤】
javascript事件捕获机制【深入分析IE和DOM中的事件模型】
php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】
如何在万网自助建站平台快速创建网站?
uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址
Python高阶函数应用_函数作为参数说明【指导】
网站建设整体流程解析,建站其实很容易!
网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?
在centOS 7安装mysql 5.7的详细教程
如何在云指建站中生成FTP站点?
如何在腾讯云免费申请建站?
如何有效防御Web建站篡改攻击?
如何解决hover在ie6中的兼容性问题
Laravel如何安装使用Debugbar工具栏_Laravel性能调试与SQL监控插件【步骤】
EditPlus中的正则表达式 实战(1)
C#如何调用原生C++ COM对象详解
在线制作视频的网站有哪些,电脑如何制作视频短片?
网站制作软件免费下载安装,有哪些免费下载的软件网站?
Laravel怎么进行浏览器测试_Laravel Dusk自动化浏览器测试入门
Laravel Blade组件怎么用_Laravel可复用视图组件的创建与使用
HTML 中如何正确使用模板变量为元素的 name 属性赋值
如何在浏览器中启用Flash_2025年继续使用Flash Player的方法【过时】
电商网站制作价格怎么算,网上拍卖流程以及规则?
Linux系统运维自动化项目教程_Ansible批量管理实战
Python函数文档自动校验_规范解析【教程】
美食网站链接制作教程视频,哪个教做美食的网站比较专业点?
深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?
个人网站制作流程图片大全,个人网站如何注销?
如何在万网主机上快速搭建网站?
php中::能调用final静态方法吗_final修饰静态方法调用规则【解答】
安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出
Laravel如何构建RESTful API_Laravel标准化API接口开发指南
详解MySQL数据库的安装与密码配置
怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?

