1

I heard that after some version of gcc using simply something like:

static A* a = new A();
return a;

Is thread-safe for a singleton and one wouldn't need something adapted from say http://locklessinc.com/articles/singleton_pattern/ anymore...

Does anyone have a specific reference or link to where I can read about this?

Jonathan Wakely
  • 153,269
  • 21
  • 303
  • 482
Palace Chan
  • 7,625
  • 5
  • 36
  • 83
  • You actually don't event require using operator new to create singletons. Therefore there's no need for critical sections. – mfontanini Jun 26 '12 at 17:17
  • 1
    @mfontanini: Using `new` avoids a destruction order fiasco, at the cost of a memory leak. There's no good way to implement a singleton in C++, but a wide choice of bad ways. – Mike Seymour Jun 26 '12 at 17:39
  • @MikeSeymour: I would still return a *reference* though... – Matthieu M. Jun 26 '12 at 17:53
  • 4
    Yeah- the thread-safe singleton pattern is: Don't use a singleton. – Puppy Jun 26 '12 at 18:25

2 Answers2

3

Section 6.7 of draft standard (n3337.pdf), point 4:

The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. 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.88 If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.

Tomek
  • 4,093
  • 1
  • 17
  • 17
1

GCC follows the cross-vendor Itanium C++ ABI. The relevant sections covering thread-safe initialization of function-local statics are 2.8 Initialization guard variables and 3.3.2 One-time Construction API, which says:

An implementation that does not anticipate supporting multi-threading may simply check the first byte (i.e., the byte with lowest address) of that guard variable, initializing if and only if its value is zero, and then setting it to a non-zero value.

However, an implementation intending to support automatically thread-safe, one-time initialization (as opposed to requiring explicit user control for thread safety) may make use of the following API functions: ...

There were some bugs in the early GCC implementation of that API, I think they are all fixed and it works correctly from GCC version 4.3 (possibly earlier, I don't recall and can't find a reference right now.)

However, Singleton is a bad, bad pattern, do not use it!

Community
  • 1
  • 1
Jonathan Wakely
  • 153,269
  • 21
  • 303
  • 482