加入线程锁

This commit is contained in:
cxh 2025-11-14 18:17:25 +08:00
parent 5cefbe6cf4
commit c95dc892b5
2 changed files with 7 additions and 0 deletions

View File

@ -57,6 +57,7 @@ class RecordManager
// === 文件列表索引 ===
std::unordered_map<std::string, std::vector<RecordFileInfo>> index_;
std::mutex index_mutex_;
// 解析文件名得到 start_ms 和 end_ms
RecordFileInfo parseFile(const std::filesystem::path& p);

View File

@ -164,6 +164,8 @@ void RecordManager::stopAutoScan()
//
void RecordManager::scanAll()
{
std::lock_guard<std::mutex> lock(index_mutex_);
index_.clear();
if (!fs::exists(record_dir_))
@ -261,6 +263,8 @@ RecordFileInfo RecordManager::parseFile(const fs::path& p)
//
std::vector<RecordSegment> RecordManager::querySegments(const std::string& stream, int64_t start_ms, int64_t end_ms)
{
std::lock_guard<std::mutex> lock(index_mutex_);
std::vector<RecordSegment> result;
if (start_ms <= 0 || end_ms <= 0 || start_ms >= end_ms) return result;
if (!index_.count(stream)) return result;
@ -322,6 +326,8 @@ std::vector<RecordSegment> RecordManager::querySegments(const std::string& strea
//
RecordSegment RecordManager::getSegment(const std::string& segmentId)
{
std::lock_guard<std::mutex> lock(index_mutex_);
// 1) 找最后一个 "_"
size_t pos_last = segmentId.rfind('_');
if (pos_last == std::string::npos) return {};