Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Fix thread safety in hnsw (#575)
Browse files Browse the repository at this point in the history
Signed-off-by: zh Wang <[email protected]>

Signed-off-by: zh Wang <[email protected]>
  • Loading branch information
hhy3 authored Dec 1, 2022
1 parent 2ac540e commit 998786c
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions thirdparty/hnswlib/hnswlib/visited_list_pool.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <algorithm>
#include <mutex>
#include <thread>
#include <unordered_map>
#include <vector>
Expand All @@ -16,6 +17,7 @@ namespace hnswlib {
class VisitedListPool {
int numelements;
std::unordered_map<std::thread::id, std::vector<bool>> map;
std::mutex mtx;

public:
VisitedListPool(int numelements1) {
Expand All @@ -24,7 +26,9 @@ class VisitedListPool {

std::vector<bool>&
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 {
Expand Down

0 comments on commit 998786c

Please sign in to comment.