diff --git a/globals.h b/globals.h index 48b6900..0d25a4d 100644 --- a/globals.h +++ b/globals.h @@ -58,13 +58,18 @@ struct DetectionResult { extern DetectionResult g_detection_result[2]; // 线程安全队列模板类 +// 线程安全队列模板类,最大长度为1避免队列数据或者结果的积压 template -class ThreadSafeQueue{ +class ThreadSafeQueue { public: - // 添加元素到队列 + // 添加元素到队列,如果队列已满(长度为1),则移除旧元素 void enqueue(const T& item) { std::unique_lock lock(mutex_); + if (queue_.size() >= 1) + { + queue_.pop(); // 移除旧的元素 + } queue_.push(item); cond_var_.notify_one(); }