Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Double-checked locking
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Usage in C++11 == For the singleton pattern, double-checked locking is not needed: {{quote|If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.|Β§ 6.7 [stmt.dcl] p4}} <syntaxhighlight lang="cpp"> Singleton& GetInstance() { static Singleton s; return s; } </syntaxhighlight> C++11 and beyond also provide a built-in double-checked locking pattern in the form of <code>std::once_flag</code> and <code>std::call_once</code>: <syntaxhighlight lang="cpp"> #include <mutex> #include <optional> // Since C++17 // Singleton.h class Singleton { public: static Singleton* GetInstance(); private: Singleton() = default; static std::optional<Singleton> s_instance; static std::once_flag s_flag; }; // Singleton.cpp std::optional<Singleton> Singleton::s_instance; std::once_flag Singleton::s_flag{}; Singleton* Singleton::GetInstance() { std::call_once(Singleton::s_flag, []() { s_instance.emplace(Singleton{}); }); return &*s_instance; } </syntaxhighlight> If one truly wishes to use the double-checked idiom instead of the trivially working example above (for instance because [[Visual Studio]] before the 2015 release did not implement the C++11 standard's language about concurrent initialization quoted above <ref>{{Cite web | url=https://msdn.microsoft.com/en-au/library/hh567368.aspx#concurrencytable | title=Support for C++11-14-17 Features (Modern C++)}}</ref> ), one needs to use acquire and release fences:<ref>[http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/ Double-Checked Locking is Fixed In C++11]</ref> <syntaxhighlight lang="cpp"> #include <atomic> #include <mutex> class Singleton { public: static Singleton* GetInstance(); private: Singleton() = default; static std::atomic<Singleton*> s_instance; static std::mutex s_mutex; }; Singleton* Singleton::GetInstance() { Singleton* p = s_instance.load(std::memory_order_acquire); if (p == nullptr) { // 1st check std::lock_guard<std::mutex> lock(s_mutex); p = s_instance.load(std::memory_order_relaxed); if (p == nullptr) { // 2nd (double) check p = new Singleton(); s_instance.store(p, std::memory_order_release); } } return p; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)