From 0bc2ec1f199cb4ea24eefc752f96fdf8982753ac Mon Sep 17 00:00:00 2001 From: fanlusky <1730976608@qq.com> Date: Wed, 30 Sep 2026 22:34:05 +0800 Subject: [PATCH] =?UTF-8?q?test(engine):=20=E9=87=8D=E6=8E=92=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E5=9C=A8=E9=94=81=E5=86=85=20notify=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E7=A2=B0=E5=88=B0=E5=B7=B2=E6=9E=90=E6=9E=84=E7=9A=84?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 等待方一拿到锁就可能离开作用域、析构栈上的 mutex 和条件变量,锁外 notify_all 会落在已析构的对象上。三个后台线程用例都改成在锁内 notify。 --- engine/tests/src/test_neural_sentence.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/engine/tests/src/test_neural_sentence.cpp b/engine/tests/src/test_neural_sentence.cpp index 5fba780d..23f8e71f 100644 --- a/engine/tests/src/test_neural_sentence.cpp +++ b/engine/tests/src/test_neural_sentence.cpp @@ -230,11 +230,10 @@ int main() bool fired = false; neural::RescoreWorker &worker = neural::RescoreWorker::instance(); worker.clear(); + // notify 放在锁内:等待方一拿到锁就会离开作用域、析构 ready,锁外 notify 可能碰到已析构的对象。 worker.set_ready_callback([&] { - { - std::lock_guard lock(mutex); - fired = true; - } + std::lock_guard lock(mutex); + fired = true; ready.notify_all(); }); @@ -278,8 +277,8 @@ int main() { std::lock_guard lock(mutex); throw_now = (++fired == 1); + done.notify_all(); } - done.notify_all(); if (throw_now) { throw std::runtime_error("the host callback failed on purpose"); @@ -323,10 +322,8 @@ int main() neural::RescoreWorker &worker = neural::RescoreWorker::instance(); worker.clear(); worker.set_ready_callback([&] { - { - std::lock_guard lock(mutex); - ++fired; - } + std::lock_guard lock(mutex); + ++fired; ready.notify_all(); });