告别驱动焦虑:一次搞懂Realtek 8852BE在Ubuntu下的内核适配与编译安装原理
2026/4/29 13:29:28
作为编程初学者,学完基础的线程创建(pthread_create)后,遇到了 “批量配置线程特征” 的场景 —— 比如让线程自动释放资源、调整线程调度优先级,这时线程属性(pthread_attr_t)就派上用场了!整理核心知识点和实战代码如下
线程属性本身不控制线程运行,而是为 pthread_create 提供统一的配置模板;核心流程:初始化属性 → 设置特征属性 → 创建线程 → 销毁属性;属性销毁后不影响已创建的线程,仅释放属性结构体资源。
| 接口 | 功能 | 关键说明 |
|---|---|---|
| pthread_attr_init | 动态初始化线程属性 | attr(属性变量指针);成功返回 0,失败返回错误码 |
| pthread_attr_destroy | 销毁线程属性 | 释放属性资源;销毁后可重新初始化;成功返回 0 |
| pthread_attr_setdetachstate | 设置线程脱离状态 | 可选值:① PTHREAD_CREATE_DETACHED(脱离态,线程结束自动释放资源,主线程不可 pthread_join)② PTHREAD_CREATE_JOINABLE(默认,主线程可 pthread_join) |
| pthread_attr_getdetachstate | 获取线程脱离状态 | 存入 detachstate 指针,用于校验属性配置结果 |
| pthread_attr_setschedpolicy | 设置调度策略 | 可选值:① SCHED_OTHER(默认,普通调度,无实时性)② SCHED_FIFO(实时 FIFO,需 sudo 运行)③ SCHED_RR(实时轮转,需 sudo 运行) |
| pthread_attr_getschedpolicy | 获取调度策略 | 存入 policy 指针,校验调度策略配置 |
| sched_get_priority_max/min | 获取优先级范围 | 配合调度策略使用;SCHED_OTHER 优先级固定为 0,SCHED_FIFO/RR 为 1~99 |
| pthread_attr_setschedparam | 设置线程优先级 | 需传入 sched_param 结构体(含 sched_priority);仅对实时策略有效 |
模拟创建 “脱离态” 线程,线程结束自动释放资源,主线程通过共享变量等待线程结束:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> int finished = 0; // 共享变量判断线程是否结束 void* thread_function(void* arg) { printf("线程开始运行...\n"); int count = (int)arg; for (int i = 0; i < count; i++) { printf("线程正在工作,第%d次\n", i + 1); sleep(1); } printf("线程即将关闭.\n"); finished = 1; pthread_exit(NULL); } int main() { pthread_attr_t thread_attr; int res; // 1. 初始化线程属性 res = pthread_attr_init(&thread_attr); if (res != 0) { perror("pthread_attr_init failed"); exit(1); } // 2. 设置为脱离状态(核心) res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if (res != 0) { perror("Setting detached attribute failed"); exit(EXIT_FAILURE); } // 3. 创建线程(传入配置好的属性) pthread_t a_thread; int n = 3; // 线程工作次数 res = pthread_create(&a_thread, &thread_attr, thread_function, (void*)n); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } // 4. 销毁属性(不影响已创建线程) pthread_attr_destroy(&thread_attr); // 脱离状态下无法pthread_join,用共享变量等待 while (!finished) { printf("等待线程结束...\n"); sleep(1); } printf("线程已结束\n"); return 0; }配置线程调度策略为默认(SCHED_OTHER),并设置优先级(仅演示流程,实时策略需 sudo 运行):
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> #include <sched.h> int finished = 0; void* thread_function(void* arg) { printf("线程开始运行...\n"); int count = (int)arg; for (int i = 0; i < count; i++) { printf("线程正在工作,第%d次\n", i + 1); sleep(1); } printf("线程即将关闭.\n"); finished = 1; pthread_exit(NULL); } int main() { pthread_attr_t thread_attr; int res; // 1. 初始化属性 res = pthread_attr_init(&thread_attr); if (res != 0) { perror("pthread_attr_init failed"); exit(1); } // 2. 设置脱离状态 res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if (res != 0) { perror("Setting detached attribute failed"); exit(EXIT_FAILURE); } // 3. 设置调度策略(SCHED_OTHER/SCHED_FIFO/SCHED_RR) res = pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER); if (res != 0) { perror("pthread_attr_setschedpolicy failed"); exit(1); } // 4. 获取优先级范围并设置 int max_priority = sched_get_priority_max(SCHED_OTHER); int min_priority = sched_get_priority_min(SCHED_OTHER); printf("SCHED_OTHER优先级范围:%d ~ %d\n", min_priority, max_priority); struct sched_param scheduling_value; scheduling_value.sched_priority = min_priority; // SCHED_OTHER仅能设0 res = pthread_attr_setschedparam(&thread_attr, &scheduling_value); if (res != 0) { perror("pthread_attr_setschedparam failed"); exit(1); } // 5. 创建线程 pthread_t a_thread; int n = 3; res = pthread_create(&a_thread, &thread_attr, thread_function, (void*)n); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } // 6. 销毁属性 pthread_attr_destroy(&thread_attr); // 等待线程结束 while (!finished) { printf("等待线程结束...\n"); sleep(1); } printf("线程已结束\n"); return 0; }线程属性依赖 pthread 库,编译时必须加-lpthread:
# 编译脱离状态示例 gcc thread_attr_detach.c -o thread_attr_detach -lpthread # 运行 ./thread_attr_detach # 编译调度策略示例(实时策略需sudo) gcc thread_attr_sched.c -o thread_attr_sched -lpthread # 普通策略运行 ./thread_attr_sched # 实时策略(如SCHED_FIFO)需sudo sudo ./thread_attr_sched线程属性是 “线程配置的统一模板”,核心价值在于批量、灵活配置线程特征:
对比直接传 NULL 给 pthread_create(默认属性),线程属性让线程配置更清晰、可维护,是 Linux 线程进阶编程的必备知识点。