#ifndef _THREAD_POOL_H_ #define _THREAD_POOL_H_ #ifndef _TYPEDEF_H_ #include "typedef.h" #endif #ifndef _PUBLIC_H_ #include "public.h" #endif #ifndef _QUEUE_H_ #include "queue.h" #endif typedef struct { int socket_fd; char ip[40]; int port; int con_timeout; int deal_timeout; int status; } threadpool_connect_info_t; typedef struct { pthread_t thread_id; queue_t* work_queue; threadpool_connect_info_t connect_info[P_DEVICE_MAX_COUNT]; int connect_count; int connect_idex; } threadpool_task_t; typedef struct { threadpool_task_t* work_threads; /* 工作线程 */ /*管理线程*/ threadpool_task_t* admin_threads; /* 管理线程 */ pthread_t admin_thread_id; /* 管理线程ID */ /*线程池信息*/ int total_thr_num; /* 线程总数 */ int live_thr_num; /* 线程池中存活的线程数 */ int busy_thr_num; /* 忙线程,正在工作的线程 */ unsigned long long task_num; /* 已执行的所有任务量统计值(不包含正在执行中的任务) */ /*任务队列信息*/ queue_t* queue[64]; int queue_max_size; /* 队列能容纳的最大任务数 */ pthread_mutex_t queue_mutex; /* 多个工作线程创建队列时加锁 */ int free_queue; /*线程池状态*/ int shutdown; /* 线程池开关: FALSE: 开启 TRUE: 关闭 */ } threadpool_t; /* 功能:创建线程池-多队列* 注意:这里的queue_max_size是一个工作线程的队列容量 */ threadpool_t* threadpool_create(int thread_num, int queue_max_size); /* 功能:添加任务-多队列 注意:该功能使用时必须加锁(必须与销毁线程(threadpool_destroy)使用相同的锁) */ int threadpool_add_task(threadpool_t *threadpool, int* (*function)(void* arg, void* device), void* arg, void* device); /* 功能:销毁线程 注意:该功能使用时必须加锁(必须与添加任务(threadpool_add_task)使用相同的锁) */ int threadpool_destroy(threadpool_t *threadpool); /*创建线程池中设备连接*/ int threadpool_create_connect( threadpool_t *threadpool, const char *pcIpList[], int iPortList[], int dev_num, int iConnectTimeout); /*断开线程池中的所有设备连接*/ int threadpool_disconnect(threadpool_t *threadpool); /*设置线程池中的连接信息*/ int threadpool_set_connect_info( threadpool_t *threadpool, const char *pcIpList[], int iPortList[], int dev_num, int iConnectTimeout, int iDealTimeOut); /*清除线程池中的连接信息*/ int threadpool_clear_connect_info(threadpool_t *threadpool); /*主备机监控恢复线程*/ int threadpool_device_admin(threadpool_t *threadpool); #endif