Skip to content

Commit 26a047a

Browse files
authored
Merge pull request #4 from isergeyam/master
Fix data race in GetInstance
2 parents 2abb1fd + 87488f1 commit 26a047a

File tree

1 file changed

+2
-5
lines changed
  • src/Singleton/Conceptual/ThreadSafe

1 file changed

+2
-5
lines changed

src/Singleton/Conceptual/ThreadSafe/main.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,10 @@ std::mutex Singleton::mutex_;
112112
*/
113113
Singleton *Singleton::GetInstance(const std::string& value)
114114
{
115+
std::lock_guard<std::mutex> lock(mutex_);
115116
if (pinstance_ == nullptr)
116117
{
117-
std::lock_guard<std::mutex> lock(mutex_);
118-
if (pinstance_ == nullptr)
119-
{
120-
pinstance_ = new Singleton(value);
121-
}
118+
pinstance_ = new Singleton(value);
122119
}
123120
return pinstance_;
124121
}

0 commit comments

Comments
 (0)