seg-database-c/inc/threadpool.h
2026-07-10 17:53:18 +08:00

103 lines
2.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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