test(engine): 重排回调在锁内 notify,避免碰到已析构的条件变量
等待方一拿到锁就可能离开作用域、析构栈上的 mutex 和条件变量,锁外 notify_all 会落在已析构的对象上。三个后台线程用例都改成在锁内 notify。
This commit is contained in:
1 parent
5d8487a490
commit
0bc2ec1f19
1 file changed
+6
-9
@@ -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<std::mutex> lock(mutex);
|
||||
fired = true;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
fired = true;
|
||||
ready.notify_all();
|
||||
});
|
||||
|
||||
@@ -278,8 +277,8 @@ int main()
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex);
|
||||
++fired;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
++fired;
|
||||
ready.notify_all();
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user