From 998786c45da0eca9854fbe5fedbaca68f2eeda55 Mon Sep 17 00:00:00 2001 From: zh Wang Date: Thu, 1 Dec 2022 18:01:12 +0800 Subject: [PATCH] Fix thread safety in hnsw (#575) Signed-off-by: zh Wang Signed-off-by: zh Wang --- thirdparty/hnswlib/hnswlib/visited_list_pool.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/thirdparty/hnswlib/hnswlib/visited_list_pool.h b/thirdparty/hnswlib/hnswlib/visited_list_pool.h index a0bcdccd1..2f0169059 100644 --- a/thirdparty/hnswlib/hnswlib/visited_list_pool.h +++ b/thirdparty/hnswlib/hnswlib/visited_list_pool.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -16,6 +17,7 @@ namespace hnswlib { class VisitedListPool { int numelements; std::unordered_map> map; + std::mutex mtx; public: VisitedListPool(int numelements1) { @@ -24,7 +26,9 @@ class VisitedListPool { std::vector& getFreeVisitedList() { + std::unique_lock lk(mtx); auto& res = map[std::this_thread::get_id()]; + lk.unlock(); if (res.size() != numelements) { res.assign(numelements, false); } else {