资源简介
一个linux下的c实现的线程池,其中包括线程池的创建、销毁、线程状态等操作。

代码片段和文件信息
#include “thread-pool.h“
static void *tp_work_thread(void *pthread);
static void *tp_manage_thread(void *pthread);
static TPBOOL tp_init(tp_thread_pool *this);
static void tp_close(tp_thread_pool *this);
static void tp_process_job(tp_thread_pool *this tp_work *worker tp_work_desc *job);
static int tp_get_thread_by_id(tp_thread_pool *this int id);
static TPBOOL tp_add_thread(tp_thread_pool *this);
static TPBOOL tp_delete_thread(tp_thread_pool *this);
static int tp_get_tp_status(tp_thread_pool *this);
/**
* user interface. creat thread pool.
* para:
* num: min thread number to be created in the pool
* return:
* thread pool struct instance be created successfully
*/
tp_thread_pool *creat_thread_pool(int min_num int max_num){
tp_thread_pool *this;
this = (tp_thread_pool*)malloc(sizeof(tp_thread_pool));
memset(this 0 sizeof(tp_thread_pool));
//init member function ponter
this->init = tp_init;
this->close = tp_close;
this->process_job = tp_process_job;
this->get_thread_by_id = tp_get_thread_by_id;
this->add_thread = tp_add_thread;
this->delete_thread = tp_delete_thread;
this->get_tp_status = tp_get_tp_status;
//init member var
this->min_th_num = min_num;
this->cur_th_num = this->min_th_num;
this->max_th_num = max_num;
pthread_mutex_init(&this->tp_lock NULL);
//malloc mem for num thread info struct
if(NULL != this->thread_info)
free(this->thread_info);
this->thread_info = (tp_thread_info*)malloc(sizeof(tp_thread_info)*this->max_th_num);
return this;
}
/**
* member function reality. thread pool init function.
* para:
* this: thread pool struct instance ponter
* return:
* true: successful; false: failed
*/
TPBOOL tp_init(tp_thread_pool *this){
int i;
int err;
//creat work thread and init work thread info
for(i=0;imin_th_num;i++){
pthread_cond_init(&this->thread_info[i].thread_cond NULL);
pthread_mutex_init(&this->thread_info[i].thread_lock NULL);
err = pthread_create(&this->thread_info[i].thread_id NULL tp_work_thread this);
if(0 != err){
printf(“tp_init: creat work thread failed\n“);
return FALSE;
}
printf(“tp_init: creat work thread %d\n“ this->thread_info[i].thread_id);
}
//creat manage thread
err = pthread_create(&this->manage_thread_id NULL tp_manage_thread this);
if(0 != err){
printf(“tp_init: creat manage thread failed\n“);
return FALSE;
}
printf(“tp_init: creat manage thread %d\n“ this->manage_thread_id);
return TRUE;
}
/**
* member function reality. thread pool entirely close function.
* para:
* this: thread pool struct instance ponter
* return:
*/
void tp_close(tp_thread_pool *this){
int i;
//close work thread
for(i=0;icur_th_num;i++){
kill(this->thread_info[i].thread_id SIGKILL);
pthread_mutex_destroy(&this->thread_info[i].thread_lock);
pthread_cond_destroy(&this->thread_info[i].thread_cond);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 74 2007-08-17 14:28 libthreadpool\AUTHORS
文件 18009 2007-08-17 14:28 libthreadpool\COPYING
目录 0 2007-08-17 14:28 libthreadpool\example
文件 168 2007-08-17 14:28 libthreadpool\INSTALL
文件 37888 2007-08-17 14:28 libthreadpool\linux线程池的C语言实现.doc
文件 640 2007-08-17 14:28 libthreadpool\Makefile
文件 107 2007-08-17 14:28 libthreadpool\README
文件 9740 2007-08-17 14:28 libthreadpool\src\thread-pool.c
文件 1873 2007-08-17 14:28 libthreadpool\src\thread-pool.h
目录 0 2007-08-17 14:28 libthreadpool\src
目录 0 2007-08-17 14:28 libthreadpool
----------- --------- ---------- ----- ----
68717 12
相关资源
- uboot到linux logo显示不间断 补丁
- UNIX/LINUX编程实践教程的源码
- Linux任务管理器
- linux应用层的华容道游戏源代码
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- linux下用多进程同步方法解决生产者
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- linux 网络实验 ftp程序
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- linux扫雷游戏代码
- CAN Linux驱动代码
- Linux系统教材
- intel 82579LM 网卡驱动Linux系统版 v1.9.
- SA1110处理器掌上电脑液晶显示器设计
- 基于Linux的串口服务器设计
- Windows下访问LINUX的利器-SSH
评论
共有 条评论