Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Double-checked locking
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Double-checked locking
(section)
Add topic