欢迎访问宝典百科,专注于IT类百科知识解答!
当前位置:宝典百科 >> 软件系统 >> linux >> 百科详情

linux中怎么创建线程池

2023-11-10 linux 责编:宝典百科 2522浏览

在Linux中,可以使用pthread库来创建和管理线程池。下面是一个简单的示例代码:

linux中怎么创建线程池

```c

#include

#include

#include

#define THREAD_POOL_SIZE 5

typedef struct {

void (*task)(void*); // 线程要执行的任务

void* arg; // 任务参数

} task_t;

typedef struct {

pthread_t* threads; // 线程数组

task_t* tasks; // 任务队列

int size; // 线程池大小

int count; // 当前任务数量

int head; // 任务队列的头

int tail; // 任务队列的尾

pthread_mutex_t mutex; // 互斥锁

pthread_cond_t not_empty; // 非空条件变量

pthread_cond_t not_full; // 非满条件变量

} thread_pool_t;

void* thread_func(void* arg) {

thread_pool_t* pool = (thread_pool_t*)arg;

while (1) {

pthread_mutex_lock(&(pool->mutex));

while (pool->count == 0) { // 如果任务队列为空,线程等待非空条件

pthread_cond_wait(&(pool->not_empty), &(pool->mutex));

}

task_t task = pool->tasks[pool->head]; // 取出头部任务

pool->head = (pool->head + 1) % pool->size;

pool->count--;

if (pool->count == pool->size - 1) { // 如果任务队列之前是满的,唤醒非满条件

pthread_cond_broadcast(&(pool->not_full));

}

pthread_mutex_unlock(&(pool->mutex));

// 执行任务

(task.task)(task.arg);

}

return NULL;

}

// 初始化线程池

void thread_pool_init(thread_pool_t* pool, int size) {

pool->threads = (pthread_t*)malloc(size * sizeof(pthread_t));

pool->tasks = (task_t*)malloc(size * sizeof(task_t));

pool->size = size;

pool->count = 0;

pool->head = 0;

pool->tail = 0;

pthread_mutex_init(&(pool->mutex), NULL);

pthread_cond_init(&(pool->not_empty), NULL);

pthread_cond_init(&(pool->not_full), NULL);

int i;

for (i = 0; i < size; i++) {

pthread_create(&(pool->threads[i]), NULL, thread_func, pool);

}

}

// 销毁线程池

void thread_pool_destroy(thread_pool_t* pool) {

int i;

for (i = 0; i < pool->size; i++) {

pthread_cancel(pool->threads[i]);

}

free(pool->threads);

free(pool->tasks);

pthread_mutex_destroy(&(pool->mutex));

pthread_cond_destroy(&(pool->not_empty));

pthread_cond_destroy(&(pool->not_full));

}

// 添加任务到线程池

void thread_pool_add_task(thread_pool_t* pool, void (*task)(void*), void* arg) {

pthread_mutex_lock(&(pool->mutex));

while (pool->count == pool->size) { // 如果任务队列已满,等待非满条件

pthread_cond_wait(&(pool->not_full), &(pool->mutex));

}

pool->tasks[pool->tail].task = task; // 添加任务到尾部

pool->tasks[pool->tail].arg = arg;

pool->tail = (pool->tail + 1) % pool->size;

pool->count++;

if (pool->count == 1) { // 如果任务队列之前是空的,唤醒非空条件

pthread_cond_broadcast(&(pool->not_empty));

}

pthread_mutex_unlock(&(pool->mutex));

}

```

使用示例:

```c

void task_func(void* arg) {

int n = *(int*)arg;

printf("Task %d is running\n", n);

usleep(1000 * n); // 模拟任务执行

}

int main() {

thread_pool_t pool;

thread_pool_init(&pool, THREAD_POOL_SIZE);

int i;

for (i = 0; i < 10; i++) {

// 创建任务参数并添加到线程池

int* arg = (int*)malloc(sizeof(int));

*arg = i;

thread_pool_add_task(&pool, task_func, arg);

}

sleep(10); // 等待所有任务执行完毕

thread_pool_destroy(&pool);

return 0;

}

```

在上面的示例中,我们使用一个简单的任务函数来模拟具体的任务。在主函数中,我们创建了一个线程池,然后添加了10个任务到线程池中,每个任务都带有一个参数。最后,我们使用sleep函数等待所有任务完成,然后销毁线程池。

本站申明:宝典百科为纯IT类百科展示网站,网站所有信息均来源于网络,若有误或侵权请联系本站!
为您推荐
  • Linux中bc是全称是什么意思在Linux操作系统中,bc是一个广泛使用的命令行计算器工具,其全称为Basic Calculator。该工具由James Gosling开发,最初作为C语言编译器的一部分被设计出来,后来演变为一个独立的命令行程序,能够执行复
    2025-10-24 linux 9325浏览
  • Linux怎么记录故障是系统管理员和开发者在维护和排查问题时的重要技能。本文将从日志系统原理、记录方法、分析技巧及存储策略等方面,系统性地阐述Linux下故障记录的全流程,并结合专业结构化数据帮助用户高效处理问题
    2025-10-23 linux 474浏览
栏目推荐
  • Linux系统的安全性在多个维度上具备显著优势,但实际安全水平取决于具体配置和使用环境。以下是详细分析:1. 开源透明性 Linux内核及主流发行版遵循GPL协议开放源代码,全球开发者可审计代码,漏洞暴露和修复速度远快于
    2025-09-18 linux 6190浏览
  • 在Linux系统中,以字母"s"开头的术语、命令或文件通常涉及以下重要概念:1. Shell:用户与内核交互的命令行界面,如Bash(Bourne-Again Shell)、Sh(Bourne Shell)等。Shell脚本以`.sh`为后缀。2. Systemd:现代Linux系统的初始化和管理工具
    2025-09-17 linux 9279浏览
  • Linux系统安装操作步骤及扩展知识:1. 准备安装介质下载ISO镜像:从官方渠道获取发行版镜像(如Ubuntu、CentOS、Debian),建议验证SHA256校验码确保完整性。制作启动盘:使用`dd`命令(Linux/Mac)或Rufus(Windows)将ISO写入U盘,U盘容
    2025-09-17 linux 4847浏览
全站推荐
  • # 三星手机怎么录屏抖音随着短视频平台的兴起,抖音成为了用户分享生活、展示创意的重要平台之一。许多用户希望将手机上的操作过程录制下来并上传到抖音,分享自己的使用体验或技巧。然而,很多人在操作三星手机录屏
    2025-11-05 三星 3440浏览
  • 日常高效的时间管理离不开精准的闹钟设定,小米手机在MIUI系统中提供了完整的闹钟功能体系。本文将通过结构化数据详解从基础设置到高阶功能的完整流程,并拓展闹钟相关实用技巧。MIUI不同版本闹钟入口路径对比MIUI版本设
    2025-11-05 小米 4323浏览
  • 怎么进行华为手机维修是许多用户关注的问题,无论是日常使用中的小故障,还是因摔落、进水导致的硬件损伤,都需要系统化的维修流程和专业化的工具支持。本文将从维修流程、工具准备、常见故障处理及技术要点等方面,
    2025-11-05 华为 9087浏览
友情链接
底部分割线