From a212ca4d4d61e65bd9371329a55870b19c30b645 Mon Sep 17 00:00:00 2001 From: karllzy Date: Thu, 26 Dec 2024 12:34:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=98=9F=E5=88=97=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=E4=B8=BA1,=E9=81=BF=E5=85=8D=E6=AE=8B=E7=95=99?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- globals.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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(); }