欢迎访问宝典百科,专注于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下使用Qt,通常有以下几个步骤: 1. 安装Qt开发环境Qt在Linux下的安装方式有几种,最常见的有通过包管理器安装和从Qt官网下载安装。 使用包管理器安装(以Ubuntu为例)在Ubuntu中,您可以通过以下命令安装Qt开发环境:```ba
    2025-04-23 linux 4980浏览
  • 在Linux中,修改文件夹(例如重命名、移动或更改权限等)可以使用一些常见的命令。以下是几种常见的操作:1. 重命名文件夹: 使用 `mv` 命令来重命名文件夹。 ```bash mv old_folder_name new_folder_name ``` 这个命令会将 `old_folde
    2025-04-23 linux 7238浏览
栏目推荐
  • 您提到的“Linux系统保存命令”可能需要一些具体的上下文。根据不同的场景,保存操作会有所不同。以下是一些常见的保存操作和命令:1. 在编辑器中保存文件(如 `vi` 或 `nano`): - vi/vim: - 保存并退出:`:wq` - 仅保
    2025-03-03 linux 4027浏览
  • Linux 驱动程序(Linux Device Driver)是操作系统与硬件设备之间的桥梁,负责在操作系统内核与硬件之间进行数据传输和控制。Linux 驱动可以执行以下几种关键功能: 1. 硬件控制 驱动程序允许操作系统控制硬件设备。它实现了与
    2025-03-02 linux 344浏览
  • 当然可以!你可以使用iPhone来学习Linux,但你需要一些适合在iOS设备上使用的工具和应用。以下是一些方法来在iPhone上学习和体验Linux:1. 使用终端模拟器: 你可以通过一些应用在iPhone上模拟Linux的命令行环境。比如: - a-Shell
    2025-03-02 linux 5961浏览
全站推荐
  • 听起来你在问 “uzerme 有 iOS 版吗?” 对吧?如果你是指 "UzerMe",就是那个提供在线 Windows/Linux 桌面系统的云服务平台,那么目前:- UzerMe 官方没有推出专门的 iOS 应用。- 但是,你可以直接通过 Safari、Chrome 等浏览器在 iPhone 或 i
    2025-04-27 ios 9107浏览
  • 在macOS上设置屏幕常亮有几种方法。以下是几种常见的设置方法: 1. 通过“系统设置”调整节能设置1. 点击屏幕左上角的苹果菜单(
    2025-04-27 macos 8817浏览
  • 在 Android 中添加换行符通常有两种方法,取决于你需要的场景:1. 在 `TextView` 中显示换行符: - 如果你想在 `TextView` 中显示换行符,可以使用 `\n` 来实现。例如: ```xml ``` 或者在代码中: ```java TextView textView = findViewBy
    2025-04-27 android 923浏览
友情链接
底部分割线