Skip to content

Commit 9b34f8d

Browse files
committed
fix abort issue in cpu multi-threads
1 parent 6840953 commit 9b34f8d

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

paddle/fluid/framework/scope.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Scope& Scope::NewScope() const {
5151
Variable* Scope::Var(const std::string& name) {
5252
auto* v = FindVarLocally(name);
5353
if (v != nullptr) return v;
54+
// acquire the lock when new var under this scope
55+
std::unique_lock<std::mutex> lock(mutex_);
5456
v = new Variable();
5557
vars_[name] = v;
5658
VLOG(3) << "Create variable " << name;
@@ -83,6 +85,7 @@ const Scope* Scope::FindScope(const Variable* var) const {
8385
return (parent_ == nullptr) ? nullptr : parent_->FindScope(var);
8486
}
8587
void Scope::DropKids() {
88+
std::unique_lock<std::mutex> lock(mutex_);
8689
for (Scope* s : kids_) delete s;
8790
kids_.clear();
8891
}
@@ -110,6 +113,7 @@ void Scope::DeleteScope(Scope* scope) const {
110113
}
111114

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

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

0 commit comments

Comments
 (0)