Does C++11 core language takes care of Singleton Dead Reference?

Kadir Erdem Demir

I have recently read Andrei Alexandrescu's Modern C++ Design. After reading 6. chapter, I begin to worry about our singletons at company. Since our experienced team leader writes core helper libraries like singletons etc... . I asked him if the way he handles singleton takes care of on dead reference problem ? If he used at_exit function call which is given by C core language?

He told me C++11 has singleton support and will execute CTORs and DTORs in a row that they will not be any dead reference problem. User will not have to cope with synchronization.

Even it sounds awesome I couldn't find any information which confirms him on internet. So please tell me if C++11 takes care of Dead Reference Problem for singletons and if so please explain a little what dark magic going behind ?

Mankarse

Presumably your team leader is talking about singletons implemented as follows:

T &get_value() {
   static T val;
   return val; 
}

In this case, the standard gives two guarantees. The first is that the val objects will be constructed exactly once, the first time that the flow of program execution passes the declaration of the local static variable, even if that happens simultaneously on several threads 6.7/4:

An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

Static initialisation is only allowed in the case of constants, so as long as T does not have a constexpr constructor you shouldn't have to worry (but read 3.6.2 for the full rules, in case there is some edge case that is relevant to your code).

The second guarantee is that all variables with static storage duration will be destructed in the reverse order of their construction 3.6.3/1:

Destructors (12.4) for initialized objects (that is, objects whose lifetime (3.8) has begun) with static storage duration are called as a result of returning from main and as a result of calling std::exit (18.5). Destructors for initialized objects with thread storage duration within a given thread are called as a result of returning from the initial function of that thread and as a result of that thread calling std::exit. The completions of the destructors for all initialized objects with thread storage duration within that thread are sequenced before the initiation of the destructors of any object with static storage duration. If the completion of the constructor or dynamic initialization of an object with thread storage duration is sequenced before that of another, the completion of the destructor of the second is sequenced before the initiation of the destructor of the first. If the completion of the constructor or dynamic initialization of an object with static storage duration is sequenced before that of another, the completion of the destructor of the second is sequenced before the initiation of the destructor of the first. [Note: This definition permits concurrent destruction. — end note ] If an object is initialized statically, the object is destroyed in the same order as if the object was dynamically initialized. For an object of array or class type, all subobjects of that object are destroyed before any block-scope object with static storage duration initialized during the construction of the subobjects is destroyed. If the destruction of an object with static or thread storage duration exits via an exception, std::terminate is called (15.5.1).

While this paragraph gives a lot of scope for concurrent destruction of static objects when their construction was concurrent, the main thing to take away from it is that destruction happens in the reverse order of construction.

Together, these mean means that if T val depends on some U val in another of these singleton functions, the U val will always be constructed before T val and destructed after the T val, so overall, this is a safe way of implementing singletons (unless you're doing something very crazy).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Garbage Collector: Taking care of object with a reference to singleton

From Dev

Does UUIDField's 'default' attribute takes care of the uniqueness?

From Dev

why this Singleton in c++11 does't work well?

From Dev

c++ Singleton by reference use

From Dev

AngularJS - When does a scope created by $scope.$new() removed. Does the garbage collector or Angular takes care of it?

From Dev

Does C++11 permit taking a reference to an anonymous temporary

From Dev

Does C++11 permit taking a reference to an anonymous temporary

From Dev

Golang: Does logging into file using log.Println takes care of concurrent access

From Dev

undefined reference to `roundf' - C language

From Dev

C++ Singleton: `Undefined reference to` error

From Dev

Thread-safe singleton in C++11

From Dev

c++11 Singleton is creating new instances

From Java

Capturing a reference by reference in a C++11 lambda

From Dev

Does MSP430G2553 takes care of interrupt re-entrancy or should I allocate stacks for each tasks in ISR?

From Dev

HTML: Who takes care show/hide the scrollbar?

From Dev

Singleton for service that takes a parameter

From Dev

Why does initializing non-const reference by rvalue work (in C++11)?

From Dev

Why does initializing non-const reference by rvalue work (in C++11)?

From Dev

What does synchronized in synchronized mean from Java Language Specification and How to make a dead lock using it?

From Dev

Variable Comparing With Pointer Reference, C language

From Dev

Why does the core Java library NOT use enums for implementing the singleton pattern?

From Dev

What does ** do in C language?

From Dev

universal reference c++11 code duplication

From Dev

C++11: reference-to-void?

From Dev

C++ 11 - rvalue reference variables

From Dev

Enum class C++11 by reference or value

From Dev

C++11 lambda cannot access reference

From Dev

C++11 static cast to rvalue reference

From Dev

c++11 how to use reference in vector?

Related Related

  1. 1

    Garbage Collector: Taking care of object with a reference to singleton

  2. 2

    Does UUIDField's 'default' attribute takes care of the uniqueness?

  3. 3

    why this Singleton in c++11 does't work well?

  4. 4

    c++ Singleton by reference use

  5. 5

    AngularJS - When does a scope created by $scope.$new() removed. Does the garbage collector or Angular takes care of it?

  6. 6

    Does C++11 permit taking a reference to an anonymous temporary

  7. 7

    Does C++11 permit taking a reference to an anonymous temporary

  8. 8

    Golang: Does logging into file using log.Println takes care of concurrent access

  9. 9

    undefined reference to `roundf' - C language

  10. 10

    C++ Singleton: `Undefined reference to` error

  11. 11

    Thread-safe singleton in C++11

  12. 12

    c++11 Singleton is creating new instances

  13. 13

    Capturing a reference by reference in a C++11 lambda

  14. 14

    Does MSP430G2553 takes care of interrupt re-entrancy or should I allocate stacks for each tasks in ISR?

  15. 15

    HTML: Who takes care show/hide the scrollbar?

  16. 16

    Singleton for service that takes a parameter

  17. 17

    Why does initializing non-const reference by rvalue work (in C++11)?

  18. 18

    Why does initializing non-const reference by rvalue work (in C++11)?

  19. 19

    What does synchronized in synchronized mean from Java Language Specification and How to make a dead lock using it?

  20. 20

    Variable Comparing With Pointer Reference, C language

  21. 21

    Why does the core Java library NOT use enums for implementing the singleton pattern?

  22. 22

    What does ** do in C language?

  23. 23

    universal reference c++11 code duplication

  24. 24

    C++11: reference-to-void?

  25. 25

    C++ 11 - rvalue reference variables

  26. 26

    Enum class C++11 by reference or value

  27. 27

    C++11 lambda cannot access reference

  28. 28

    C++11 static cast to rvalue reference

  29. 29

    c++11 how to use reference in vector?

HotTag

Archive