Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the lazy mode and hungry man model of C++ singleton model

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to realize the lazy mode and hungry mode of C++ singleton mode". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to realize the lazy mode and hungry man mode of C++ singleton mode" can help you solve your doubts.

Lazy man mode

The lazy pattern is not instantiated until the first time the class instance is used, but the object of this class does not exist until the getInstance function is called. The slacker itself is not thread-safe.

# include using namespace std;class Singelton {private: Singelton () {m_count + +; printf ("Singelton begin\ n"); Sleep (1000); / / add sleep to magnify the effect printf ("Singelton end\ n");} static Singelton * single;// defines a unique pointer to the instance and is private public: static Singelton * GetSingelton () / / define a public function that can get the unique instance static void print (); static int mcount;}; / / initialize the unique pointer to the instance to nullptrSingelton * Singelton::single = nullptr;int Singelton::m_count = 0 single Singelton * Singelton::GetSingelton () {if (single = = nullptr) {/ / determine whether it is the first time to use single = new Singelton;} return single } void Singelton::print () {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report