圆月山庄资源网 Design By www.vgjia.com
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; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月09日
2024年11月09日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]