@@ -51,6 +51,8 @@ Scope& Scope::NewScope() const {
51
51
Variable* Scope::Var (const std::string& name) {
52
52
auto * v = FindVarLocally (name);
53
53
if (v != nullptr ) return v;
54
+ // acquire the lock when new var under this scope
55
+ std::unique_lock<std::mutex> lock (mutex_);
54
56
v = new Variable ();
55
57
vars_[name] = v;
56
58
VLOG (3 ) << " Create variable " << name;
@@ -83,6 +85,7 @@ const Scope* Scope::FindScope(const Variable* var) const {
83
85
return (parent_ == nullptr ) ? nullptr : parent_->FindScope (var);
84
86
}
85
87
void Scope::DropKids () {
88
+ std::unique_lock<std::mutex> lock (mutex_);
86
89
for (Scope* s : kids_) delete s;
87
90
kids_.clear ();
88
91
}
@@ -110,6 +113,7 @@ void Scope::DeleteScope(Scope* scope) const {
110
113
}
111
114
112
115
void Scope::EraseVars (const std::vector<std::string>& var_names) {
116
+ std::unique_lock<std::mutex> lock (mutex_);
113
117
std::set<std::string> var_set (var_names.begin (), var_names.end ());
114
118
for (auto it = vars_.begin (); it != vars_.end ();) {
115
119
if (var_set.find (it->first ) != var_set.end ()) {
@@ -140,6 +144,8 @@ std::string Scope::Rename(const std::string& origin_name) const {
140
144
}
141
145
142
146
Variable* Scope::FindVarLocally (const std::string& name) const {
147
+ // acquire the lock when find locally
148
+ std::unique_lock<std::mutex> lock (mutex_);
143
149
auto it = vars_.find (name);
144
150
if (it != vars_.end ()) return it->second ;
145
151
return nullptr ;
0 commit comments