-
Notifications
You must be signed in to change notification settings - Fork 1
Utils_README
SweerItTer edited this page Feb 21, 2026
·
4 revisions
Utils 模块提供通用的工具类,包括线程管理、内存管理、队列、日志、设备监控等功能。
| 类 | 文档 | 说明 |
|---|---|---|
AsyncThreadPool |
Utils_AsyncThreadPool.md | 异步线程池 |
ThreadPauser |
Utils_ThreadPauser.md | 线程暂停器 |
ThreadUtils |
Utils_ThreadUtils.md | 线程工具 |
| 类 | 文档 | 说明 |
|---|---|---|
FixedSizePool |
Utils_FixedSizePool.md | 固定大小对象池 |
ObjectsPool |
Utils_ObjectsPool.md | 对象池 |
SharedBufferState |
Utils_SharedBufferState.md | 共享缓冲区状态 |
| 类 | 文档 | 说明 |
|---|---|---|
SafeQueue |
Utils_SafeQueue.md | 线程安全队列 |
OrderedQueue |
Utils_OrderedQueue.md | 有序队列 |
ConcurrentQueue |
Utils_ConcurrentQueue.md | 并发队列 |
| 类 | 文档 | 说明 |
|---|---|---|
FdWrapper |
Utils_FdWrapper.md | 文件描述符包装器 |
FenceWatcher |
Utils_FenceWatcher.md | Fence 监控器 |
| 类 | 文档 | 说明 |
|---|---|---|
Logger |
Utils_Logger.md | 日志系统 |
CpuMonitor |
Sys_CpuMonitor.md | CPU 监控 |
MemoryMonitor |
Sys_MemoryMonitor.md | 内存监控 |
UdevMonitor |
Utils_UdevMonitor.md | 设备监控 |
| 类 | 文档 | 说明 |
|---|---|---|
ProgressBar |
Utils_ProgressBar.md | 进度条 |
SimpleVariant |
Utils_SimpleVariant.md | 简单变体 |
Types |
Utils_Types.md | 类型定义 |
- 异步任务执行
- 线程池管理
- 线程暂停和恢复
- 对象池管理
- 缓冲区状态管理
- 引用计数管理
- 线程安全队列
- 阻塞和非阻塞模式
- 超时等待
- 设备热插拔监控
- 设备事件回调
- 设备发现
- 多级别日志输出
- 日志文件管理
- 线程安全
// 创建线程池
AsyncThreadPool pool(4);
// 提交任务
auto future = pool.submit([](int a, int b) {
return a + b;
}, 10, 20);
// 获取结果
int result = future.get();
printf("Result: %d\n", result);// 创建内存池,分配 16 字节大小的内存块
FixedSizePool pool(16);
// 分配内存
void* ptr = pool.allocate();
if (ptr) {
// 使用内存
int* value = new(ptr) int(42); // 在分配的内存上构造对象
*value = 42;
printf("Value: %d\n", *value);
// 销毁对象
value->~int();
// 释放内存
pool.deallocate(ptr);
}// 创建共享缓冲区
auto shared_state = std::make_shared<SharedBufferState>(dma_fd, 2);
// 设置有效
shared_state->setValid(true);
// 检查有效性
if (shared_state->getValid()) {
int fd = shared_state->fd();
process_buffer(fd);
}// 创建队列
SafeQueue<int> queue;
// 入队
queue.push(42);
// 出队(阻塞)
int item = queue.pop();
// 出队(非阻塞)
auto item = queue.tryPop();
if (item) {
printf("Got item: %d\n", *item);
}
// 出队(超时)
auto item = queue.tryPopFor(std::chrono::seconds(1));// 设置日志级别
Logger::instance().setLevel(LogLevel::INFO);
// 设置日志文件
Logger::instance().setLogFile("/var/log/myapp.log");
// 记录日志
LOG_INFO("Application started");
LOG_DEBUG("Debugging information: value=%d", 42);
LOG_WARNING("Warning: low memory");
LOG_ERROR("Error: failed to connect");// 监控 video4linux 设备
UdevMonitor monitor("video4linux");
monitor.setCallback([](const UdevMonitor::DeviceInfo& info) {
if (info.event == UdevMonitor::EventType::ADD) {
printf("Device added: %s\n", info.devnode.c_str());
} else if (info.event == UdevMonitor::EventType::REMOVE) {
printf("Device removed: %s\n", info.devnode.c_str());
}
});
monitor.start();- AsyncThreadPool: 线程安全,可以并发提交任务
- FixedSizePool: 线程安全,使用线程本地缓存和互斥锁保护全局数据结构
- SafeQueue: 线程安全,所有操作都是线程安全的
- SharedBufferState: 线程安全,valid 标志使用原子操作
- Logger: 线程安全,可以并发记录日志
- UdevMonitor: 线程安全,回调在监控线程中调用
- 使用对象池减少内存分配开销
- 使用线程池提高并行度
- 使用移动语义减少拷贝
- 使用条件变量提高效率
- 检查所有函数的返回值
- 使用异常处理错误
- 记录错误日志
- 线程安全: 确保线程安全的使用方式
- 资源管理: 使用 RAII 管理资源
- 性能: 合理选择数据结构和算法
- 异常处理: 检查返回值,处理异常
- 日志: 记录重要的调试信息
- 线程数: 根据硬件配置设置合理的线程数
- 队列大小: 根据需求设置合适的队列大小
- 对象池: 频繁分配的对象使用对象池
- v1.0 - 初始版本,提供基本的工具类
主页
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 监视器