File tree Expand file tree Collapse file tree
singleton-smart-pointer-example Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,3 +6,4 @@ add_subdirectory(singleton-meyers-example)
66add_subdirectory (singleton-classic-static-example )
77add_subdirectory (singleton-classic-dynamic-example )
88add_subdirectory (singleton-dclp-example )
9+ add_subdirectory (singleton-smart-pointer-example )
Original file line number Diff line number Diff line change 1+ cmake_minimum_required (VERSION 3.21 )
2+ project (singleton-smart-pointer-example VERSION 0.1.0 LANGUAGES CXX )
3+
4+ # -------------------------------------------------------------
5+ # Executable
6+ # -------------------------------------------------------------
7+ add_executable (singleton-smart-pointer-example
8+ main.cpp
9+ src/singleton.cpp
10+ )
11+
12+ # -------------------------------------------------------------
13+ # Compiler Features
14+ # -------------------------------------------------------------
15+ target_compile_features (singleton-smart-pointer-example PRIVATE cxx_std_17 )
16+
17+ set_target_properties (singleton-smart-pointer-example PROPERTIES
18+ CXX_STANDARD_REQUIRED ON
19+ CXX_EXTENSIONS OFF
20+ )
21+
22+ # -------------------------------------------------------------
23+ # Include directories
24+ # -------------------------------------------------------------
25+ # PRIVATE means: Only this target needs 'inc' to build.
26+ target_include_directories (singleton-smart-pointer-example PRIVATE
27+ inc
28+ )
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ class Singleton {
4+ public:
5+ Singleton (const Singleton&) = delete ; /* Deleted copy constructor. */
6+ Singleton& operator =(const Singleton&) = delete ; /* Deleted copy assigment operator. */
7+
8+ static Singleton& getInstance ();
9+ void func ();
10+ ~Singleton ();
11+
12+ private:
13+ Singleton ();
14+ };
Original file line number Diff line number Diff line change 1+ #include " singleton.h"
2+ #include < iostream>
3+ #include < vector>
4+ #include < thread>
5+
6+ /*
7+ * Singleton with local static unique pointer
8+ * Dynamic memory allocation
9+ * Lazy initialization
10+ * Singleton is created in the first call of getInstance() and destroyed automatically after main() call
11+ * Thread-safety IS guaranteed (C++11+)
12+ */
13+
14+ int main ()
15+ {
16+ std::cout << " --- main start ---" << std::endl;
17+
18+ std::vector<std::thread> threads;
19+
20+ // Launch 10 threads
21+ for (int i = 0 ; i < 10 ; ++i) {
22+ threads.emplace_back ([]() {
23+ Singleton::getInstance ().func ();
24+ });
25+ }
26+
27+ for (auto & t : threads) {
28+ t.join ();
29+ }
30+
31+ std::cout << " --- main end ---" << std::endl;
32+ }
Original file line number Diff line number Diff line change 1+ #include " singleton.h"
2+ #include < iostream>
3+ #include < memory>
4+
5+ Singleton& Singleton::getInstance () {
6+ // The "Magic Static" is thread-safe in C++11.
7+ static std::unique_ptr<Singleton> instance (new Singleton ());
8+ return *instance;
9+ }
10+
11+ Singleton::Singleton () {
12+ std::cout << " Singleton created." << std::endl;
13+ }
14+
15+ void Singleton::func () {
16+ std::cout << " Doing something..." << std::endl;
17+ }
18+
19+ Singleton::~Singleton () {
20+ std::cout << " Singleton destroyed." << std::endl;
21+ }
You can’t perform that action at this time.
0 commit comments