The thread safety of `RunCallback`
ZiyueHuang opened this issue · comments
Is KVWorker::RunCallback thread safe?
According to iterator invalidation rules, rehashing invalidates the iterators of unordered_map
, which may occur during the execution of the callback (it->second();
). I didn't encounter any error about "invalid iterator" in my application, but I think it would be better to fix this,
Callback cb = nullptr;
mu_.lock();
auto it = callbacks_.find(timestamp);
if (it != callbacks_.end()) {
cb = it->second;
CHECK(cb);
callbacks_.erase(it);
}
mu_.unlock();
if (cb) cb();