-
Notifications
You must be signed in to change notification settings - Fork 2
Utils_ThreadUtils
SweerItTer edited this page Feb 21, 2026
·
4 revisions
ThreadUtils 是 utilsCore Utils 模块的线程工具类,提供线程亲和性绑定和实时优先级设置功能。
- 绑定线程到指定 CPU 核心
- 设置线程实时优先级
- 提供重试机制
- 线程性能优化
- 性能优化(绑定线程到特定核心)
- 实时任务调度
- 降低缓存竞争
- 提高系统吞吐量
-
Linux 平台: 使用
pthread_setaffinity_np和pthread_setschedparam - ARM/x86: 支持 RK356x 等嵌入式平台
- 依赖: Linux pthread 库
- 被依赖: DisplayManager, VisionPipeline, UIRenderer 等模块
ThreadUtils 是静态工具类,提供:
- 线程 CPU 核心绑定
- 线程亲和性设置
- 实时优先级设置
- 重试机制
- 工具类模式: 所有方法都是静态方法
- 策略模式: 提供不同的绑定策略(简单绑定、带重试绑定)
static bool safeBindThread(std::thread& thread, int cpuCore, int retries = 3)参数说明:
-
thread(输入): 要绑定的 std::thread 引用 -
cpuCore(输入): CPU 核心编号(0-based) -
retries(输入): 重试次数(默认 3)
返回值:
-
true: 绑定成功 -
false: 重试次数用尽后失败
所有权归属:
- 不转移线程的所有权
注意事项:
- 如果线程未 joinable,会等待 10ms 后重试
- 每次重试失败会等待 10ms
- 适用于可能未完全初始化的线程
- 控制台输出: 成功时会输出 "Successfully bound thread to core X" 到标准输出
- 控制台输出: 失败时会输出 "Failed to bind thread to core X" 到标准输出
使用例程:
std::thread t([]() {
// 工作线程代码
});
// 安全绑定到 CPU 核心 0,最多重试 3 次
if (ThreadUtils::safeBindThread(t, 0, 3)) {
// 方法内部已输出成功信息: "Successfully bound thread to core 0"
printf("Additional success message\n");
} else {
// 方法内部已输出失败信息: "Failed to bind thread to core 0"
printf("Additional failure message\n");
}
t.join();static bool bindCurrentThreadToCore(int cpuCore)参数说明:
-
cpuCore(输入): CPU 核心编号(0-based)
返回值:
-
true: 绑定成功 -
false: 绑定失败
所有权归属:
- 无所有权转移
注意事项:
- 绑定调用线程自身
- 适用于主线程或已启动的线程
- 使用
pthread_self()获取当前线程 ID - 控制台输出: 成功时会输出 "Successfully bound current thread to core X" 到标准输出
使用例程:
// 在主线程中调用
if (ThreadUtils::bindCurrentThreadToCore(2)) {
// 方法内部已输出成功信息: "Successfully bound current thread to core 2"
printf("Additional success message\n");
}
// 在工作线程中调用
std::thread t([]() {
if (ThreadUtils::bindCurrentThreadToCore(1)) {
// 方法内部已输出成功信息: "Successfully bound current thread to core 1"
printf("Worker thread additional message\n");
}
// 工作代码
});static void bindThreadToCore(std::thread& thread, int core)参数说明:
-
thread(输入): 要绑定的 std::thread 引用 -
core(输入): CPU 核心编号(0-based)
返回值: 无(void)
所有权归属:
- 不转移线程的所有权
注意事项:
- 简单绑定,不提供重试机制
- 失败时不会抛出异常
- 如果需要错误处理,使用
safeBindThread()
使用例程:
std::thread t([]() {
// 工作线程代码
});
// 直接绑定到 CPU 核心 3
ThreadUtils::bindThreadToCore(t, 3);
t.join();static void setRealtimeThread(pthread_t stl_thread_handle, int priority)参数说明:
-
stl_thread_handle(输入): pthread 线程句柄 -
priority(输入): 优先级(1-99,数值越大优先级越高)
返回值: 无(void)
所有权归属:
- 无所有权转移
注意事项:
- 使用 SCHED_FIFO 调度策略
- 优先级范围:1-99
- 需要适当的权限(CAP_SYS_NICE)
- 失败时会打印错误信息(perror)
- 实时优先级适用于关键任务
使用例程:
std::thread t([]() {
// 关键任务代码
});
// 设置为实时线程,优先级 80
ThreadUtils::setRealtimeThread(t.native_handle(), 80);
t.join();// src/pipeline/displayManager.cpp:62-63
void DisplayManager::Impl::start() {
if (running.exchange(true)) return;
thread = std::thread(&DisplayManager::Impl::mainLoop, this);
ThreadUtils::bindThreadToCore(thread, 3); // 绑定到 CORE 3
ThreadUtils::setRealtimeThread(thread.native_handle(), 80); // 设置高优先级
}// src/pipeline/visionPipeline.cpp:156-157
void VisionPipeline::Impl::start() {
resume();
if (running.exchange(true)) return;
camera->start();
camera->setThreadAffinity(2); // 绑定采集线程到 CORE 2
thread = std::thread(&VisionPipeline::Impl::mainLoop, this);
ThreadUtils::bindThreadToCore(thread, 1);
ThreadUtils::setRealtimeThread(thread.native_handle(), 81);
}// src/pipeline/uiRenderer.cpp:83
void UIRenderer::Impl::start() {
if (!inited) init();
Q_ASSERT(QThread::currentThread() == qApp->thread());
// 绑定线程到(主线程) CPU CORE 2
ThreadUtils::bindCurrentThreadToCore(2);
// ...
}- 使用
pthread_setaffinity_np设置线程亲和性 -
cpu_set_t结构体指定线程可运行的 CPU 核心 - 绑定后线程只能运行在指定的核心上
- 减少缓存竞争: 不同线程绑定不同核心,减少缓存失效
- 提高缓存命中率: 线程始终在同一核心运行,缓存更有效
- 降低上下文切换: 减少线程迁移带来的开销
- 使用
SCHED_FIFO调度策略 - 高优先级线程优先运行
- 适用于关键任务(如视频渲染、采集)
// 采集线程绑定到 CORE 2
std::thread captureThread([]() {
ThreadUtils::bindCurrentThreadToCore(2);
while (running) {
captureFrame();
}
});
// 处理线程绑定到 CORE 1
std::thread processThread([]() {
ThreadUtils::bindCurrentThreadToCore(1);
while (running) {
processFrame();
}
});
// 显示线程绑定到 CORE 3
std::thread displayThread([]() {
ThreadUtils::bindCurrentThreadToCore(3);
while (running) {
displayFrame();
}
});std::thread t([]() {
// 工作代码
});
// 等待线程完全启动后绑定
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (ThreadUtils::safeBindThread(t, 0, 5)) {
printf("Bound successfully\n");
} else {
printf("Binding failed after 5 retries\n");
}
t.join();// 视频采集线程(高优先级)
std::thread captureThread([]() {
ThreadUtils::bindCurrentThreadToCore(2);
ThreadUtils::setRealtimeThread(pthread_self(), 90);
while (running) {
captureFrame();
}
});
// 视频处理线程(中优先级)
std::thread processThread([]() {
ThreadUtils::bindCurrentThreadToCore(1);
ThreadUtils::setRealtimeThread(pthread_self(), 70);
while (running) {
processFrame();
}
});- CPU 核心编号: 从 0 开始,不能超过实际核心数
- 权限要求: 设置实时优先级需要 CAP_SYS_NICE 权限
- 线程状态: 线程必须 joinable 才能绑定
-
重试机制:
safeBindThread()适用于未完全初始化的线程 - 实时策略: SCHED_FIFO 是抢占式调度,可能影响其他线程
- 优先级范围: 1-99,数值越大优先级越高
- 跨平台: 仅支持 Linux 平台
- 性能测试: 绑定核心不一定总是提升性能,需要测试验证
- ThreadPauser - 线程暂停控制
- AsyncThreadPool - 异步线程池
- Utils 模块总览
主页
API 文档
DMA 模块
DRM 模块
- DRM 模块总览
- DeviceController - DRM 设备控制器
- DrmLayer - DRM 图层管理
- PlanesCompositor - DRM 平面合成器
- DrmBpp - DRM 格式定义
NET 模块
- NET 模块总览
- TcpServer - TCP 服务器
- SocketConnection - Socket 连接管理
- CommandHandler - 命令处理器
- DataPacket - 数据包
V4L2 模块
- V4L2 模块总览
- CameraController - V4L2 摄像头控制器
- Frame - V4L2 帧数据结构
- FormatTool - V4L2 格式工具
- Exception - V4L2 异常类
V4L2Param 模块
- V4L2Param 模块总览
- ParamControl - 参数控制
- ParamLogger - 参数日志
- ParamProcessor - 参数处理器
RGA 模块
- RGA 模块总览
- RgaConverter - RGA 转换器
- RgaProcessor - RGA 处理器
- FormatTool - RGA 格式工具
MPP 模块
- MPP 模块总览
- EncoderContext - 编码器上下文
- EncoderCore - 编码器核心
- JpegEncoder - JPEG 编码器
- StreamWriter - 流写入器
- MppResourceGuard - MPP 资源守护
- FileTools - 文件工具
- FormatTool - 格式工具
Sys 模块
- Sys 模块总览
- CpuMonitor - CPU 监控器
- MemoryMonitor - 内存监控器
- Base - 基础类
Mouse 模块
- Mouse 模块总览
- Watcher - 鼠标监视器
Utils 模块
- Utils 模块总览
- AsyncThreadPool - 异步线程池
- ConcurrentQueue - 并发队列
- FdWrapper - 文件描述符包装器
- FenceWatcher - 围栏监视器
- FixedSizePool - 固定大小对象池
- Logger - 日志记录器
- ObjectsPool - 对象池
- OrderedQueue - 有序队列
- ProgressBar - 进度条
- SafeQueue - 安全队列
- SharedBufferState - 共享缓冲区状态
- SimpleVariant - 简单变体类型
- ThreadPauser - 线程暂停器
- ThreadUtils - 线程工具
- Types - 类型定义
- UdevMonitor - Udev 监视器