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 avoid singletons in C++

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to avoid singletons on C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn "how to avoid singletons on C++".

I.3: Avoid singletons (avoid singleton) Reason (reason)

Singletons are basically complicated global objects in disguise.

A singleton is basically a camouflage of a complex global object.

Example (sample)

Class Singleton {/ /... Lots of stuff to ensure that only one Singleton object is created, / / that it is initialized properly, etc.}

There are many variants of the singleton idea. That's part of the problem.

There are many versions of singleton ideas. This is one aspect of the problem.

Note (Note)

If you don't want a global object to change, declare it const or constexpr.

If you don't want the global object to change, define it as a constant or a constant expression.

Exception (exception)

You can use the simplest "singleton" (so simple that it is often not considered a singleton) to get initialization on first use, if any:

You can use the simplest "singleton" (so simple that it is often not considered a singleton) to get the initial value the first time you use it.

X & myX () {static X my_x {3}; return my_x;}

This is the most effective way to solve the problems related to initialization order. In a multithreaded environment, the initialization of a static object does not trigger a race condition (unless you accidentally access it in the constructor of a shared object).

Translator's note: this is also the latest implementation of the singleton design pattern in modern C++.

Note that local static variables do not contain competition conditions. However, if the destructing action of X involves operations that need to be synchronized, we must use a sub-simple solution. For example:

X & myX () {static auto p = new X {3}; return * p; / / potential leak}

Now someone must delete that object in some suitably thread-safe way. That's error-prone, so we don't use that technique unless

Now a role must delete that object in an appropriate thread-safe manner. Because it is easy to cause errors, we will not use this technique unless

MyX is in multi-threaded code

MyX is in multithreaded code.

That X object needs to be destroyed (e.g., because it releases a resource), and

The X object needs to be destroyed (for example, because it releases resources)

X's destructor's code needs to be synchronized.

The code for the destructor of X needs to be synchronized.

Translator's note: static objects built inside the function will be destructed automatically when the program exits. If you want to control the timing of destructing, you can only build the object dynamically to avoid automatic destructing.

If you, as many do, define a singleton as a class for which only one object is created, functions like myX are not singletons, and this useful technique is not an exception to the no-singleton rule.

If, like many people, you define singleton classes for objects that create only one, and functions like myX do not use singletons, this useful technique is not an exception to the singleton specification.

Translator's note: doing a practice is not a single case, and it will not violate the rule of prohibiting singles.

Enforcement (implementation recommendations)

Very hard in general.

It's usually very difficult.

Look for classes with names that include singleton.

Find the class whose name contains singletong.

Look for classes for which only a single object is created (by counting objects or by examining constructors).

Find a class that creates only one object (by counting or checking (monitoring) the object constructor)

If a class X has a public static function that contains a function-local static of the class' type X and returns a pointer or reference to it, ban that.

If class X has an exposed static function, disable it when there is a local static variable of type X with scope for that function and returns a pointer or reference to that variable.

Thank you for your reading. The above is the content of "how to avoid a singleton on C++". After the study of this article, I believe you have a deeper understanding of how to avoid a singleton on C++. The specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report