Skip to content

Instantly share code, notes, and snippets.

@Keruspe
Created December 17, 2015 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keruspe/eb48606f952ca3e3b8af to your computer and use it in GitHub Desktop.
Save Keruspe/eb48606f952ca3e3b8af to your computer and use it in GitHub Desktop.
stdin
diff --git a/foo.cc b/foo.cc
index 3c4f06c..51e8965 100644
--- a/foo.cc
+++ b/foo.cc
@@ -33,11 +33,13 @@ Bank* Bank::global_instance = nullptr;
// Singleton instance of the bank
Bank* Bank::instance() {
// avoid multiple creations of a bank instance
- global_lock.lock();
if (global_instance == nullptr) {
- global_instance = new Bank;
+ global_lock.lock();
+ if (global_instance == nullptr) {
+ global_instance = new Bank;
+ }
+ global_lock.unlock();
}
- global_lock.unlock();
return global_instance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment