dmlc / ps-lite

A lightweight parameter server interface

Home Page:http://ps-lite.readthedocs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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();

cc @eric-haibin-lin

Let me add more context. ZPush/ZPull insert new elements to callbacks_, while RunCallback eliminates some elements from callbacks_ and it is called inside Process on another thread created by Customer at this line and this line.