helloworld:design_pattern:singleton:cplusplus
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| helloworld:design_pattern:singleton:cplusplus [2016/12/03 22:52] – Création root | helloworld:design_pattern:singleton:cplusplus [2022/07/04 10:06] (Version actuelle) – [Solution] : lock_guard->scoped_lock root | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====Problème==== | ||
| + | [[http:// | ||
| + | |||
| + | [[http:// | ||
| + | |||
| < | < | ||
| Consider again pInstance = new Singleton;, the line that initializes pInstance. This statement causes three things to happen: | Consider again pInstance = new Singleton;, the line that initializes pInstance. This statement causes three things to happen: | ||
| Ligne 5: | Ligne 10: | ||
| * Step 3. Make pInstance point to the allocated memory. | * Step 3. Make pInstance point to the allocated memory. | ||
| Of critical importance is the observation that compilers are not constrained to perform these steps in this order! **In particular, compilers are sometimes allowed to swap Steps 2 and 3**. Why they might want to do that is a question we'll address in a moment. For now, let's focus on what happens if they do. | Of critical importance is the observation that compilers are not constrained to perform these steps in this order! **In particular, compilers are sometimes allowed to swap Steps 2 and 3**. Why they might want to do that is a question we'll address in a moment. For now, let's focus on what happens if they do. | ||
| - | < | + | < |
| + | < | ||
| + | </ | ||
| En gros, le pointeur peut être différent de '' | En gros, le pointeur peut être différent de '' | ||
| - | Solution | + | ====Solution==== |
| + | [[http:// | ||
| + | |||
| + | |||
| + | [[http:// | ||
| + | |||
| + | Si le singleton possède un constructeur trivial | ||
| <code cpp> | <code cpp> | ||
| std:: | std:: | ||
| Ligne 15: | Ligne 28: | ||
| Singleton* Singleton:: | Singleton* Singleton:: | ||
| - | Singleton* tmp = m_instance.load(std:: | + | Singleton* tmp = m_instance.load(std:: |
| - | | + | |
| if (tmp == nullptr) { | if (tmp == nullptr) { | ||
| - | std::lock_guard< | + | std::scoped_lock< |
| tmp = m_instance.load(std:: | tmp = m_instance.load(std:: | ||
| if (tmp == nullptr) { | if (tmp == nullptr) { | ||
| tmp = new Singleton; | tmp = new Singleton; | ||
| - | | + | m_instance.store(tmp, |
| - | | + | |
| } | } | ||
| } | } | ||
| return tmp; | return tmp; | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | Si constructeur non trivial : | ||
| + | <code cpp> | ||
| + | class MySingleton{ | ||
| + | public: | ||
| + | static MySingleton& | ||
| + | std:: | ||
| + | return *instance; | ||
| + | } | ||
| + | private: | ||
| + | static MySingleton* instance; | ||
| + | static std:: | ||
| + | |||
| + | static void initSingleton() { | ||
| + | instance= new MySingleton; | ||
| + | } | ||
| + | }; | ||
| </ | </ | ||
helloworld/design_pattern/singleton/cplusplus.1480801959.txt.gz · Dernière modification : de root
