欢迎访问宝典百科,专注于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系统的推广需要从技术、生态、教育和市场等多个维度切入,结合其开源特性与社区优势进行系统性推进:1. 降低使用门槛优化硬件兼容性:联合主流硬件厂商(如戴尔、联想)预装Linux系统,确保主流笔记本、显卡、外设
    2025-09-02 linux 4758浏览
  • 在Linux系统中通过SSH连接到路由器,需遵循以下步骤及注意事项: 1. 确认路由器支持SSH - 主流路由器如OpenWRT、DD-WRT、华硕梅林固件等通常内置SSH服务,需在路由器管理界面启用: - 进入路由器后台(通常通过浏览器访问`192.
    2025-09-02 linux 6056浏览
栏目推荐
  • 在Linux环境下升级BIOS通常需要以下几个步骤,不同主板厂商的具体操作可能有所差异,但总体流程类似:1. 确认当前BIOS版本 使用以下命令查看当前BIOS版本(需安装`dmidecode`工具): bash sudo dmidecode -t bios 记录版本号
    2025-07-28 linux 7615浏览
  • 在单个硬盘上安装两个Linux系统需要考虑分区规划、引导管理和文件系统兼容性。以下是详细技术方案: 1. 分区规划建议GPT分区表:优先采用GPT而非MBR,支持超过4个主分区且可靠性更高。独立根分区:为每个Linux分配独立根分区
    2025-07-27 linux 975浏览
  • 在Linux虚拟机中配置网络连接可通过以下几种方式实现,具体选择取决于宿主机的网络环境和虚拟化平台(如VMware、VirtualBox、KVM等):1. 桥接模式(Bridged Networking) - 虚拟机会直接连接到物理网络,与宿主机并列获得独立IP,
    2025-07-27 linux 1909浏览
全站推荐
  • 目前中国法律法规严格禁止传播和下载盗版影视资源,"飞快影视"等未获授权平台存在法律风险、安全隐患,且无官方iOS版本。若需合法使用影视服务,可通过以下途径:1. 正版平台下载 - 腾讯视频、爱奇艺、优酷、芒果TV等
    2025-09-03 ios 228浏览
  • 在macOS中合并两个日志式(HFS+或APFS格式)的磁盘需要谨慎操作,以避免数据丢失。以下是详细步骤和注意事项: 1. 备份数据 操作前务必使用Time Machine或手动复制方式备份两个磁盘上的所有数据。磁盘合并可能导致数据覆盖或
    2025-09-03 macos 1853浏览
  • 在Android系统中设置环境变量涉及多个层面,包括ADB调试环境、开发环境配置以及系统级变量的修改。以下是具体方法和扩展知识: 1. ADB和开发者工具环境变量(Windows/macOS/Linux)作用:方便在终端直接调用ADB、Fastboot等工具。步
    2025-09-03 android 8166浏览
友情链接
底部分割线