Skip to content

Commit

Permalink
fix abort issue in cpu multi-threads
Browse files Browse the repository at this point in the history
  • Loading branch information
tensor-tang committed Jun 6, 2018
1 parent 6840953 commit 9b34f8d
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions paddle/fluid/framework/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Scope& Scope::NewScope() const {
Variable* Scope::Var(const std::string& name) {
auto* v = FindVarLocally(name);
if (v != nullptr) return v;
// acquire the lock when new var under this scope
std::unique_lock<std::mutex> lock(mutex_);
v = new Variable();
vars_[name] = v;
VLOG(3) << "Create variable " << name;
Expand Down Expand Up @@ -83,6 +85,7 @@ const Scope* Scope::FindScope(const Variable* var) const {
return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
}
void Scope::DropKids() {
std::unique_lock<std::mutex> lock(mutex_);
for (Scope* s : kids_) delete s;
kids_.clear();
}
Expand Down Expand Up @@ -110,6 +113,7 @@ void Scope::DeleteScope(Scope* scope) const {
}

void Scope::EraseVars(const std::vector<std::string>& var_names) {
std::unique_lock<std::mutex> lock(mutex_);
std::set<std::string> var_set(var_names.begin(), var_names.end());
for (auto it = vars_.begin(); it != vars_.end();) {
if (var_set.find(it->first) != var_set.end()) {
Expand Down Expand Up @@ -140,6 +144,8 @@ std::string Scope::Rename(const std::string& origin_name) const {
}

Variable* Scope::FindVarLocally(const std::string& name) const {
// acquire the lock when find locally
std::unique_lock<std::mutex> lock(mutex_);
auto it = vars_.find(name);
if (it != vars_.end()) return it->second;
return nullptr;
Expand Down

0 comments on commit 9b34f8d

Please sign in to comment.