0

All!

I am new to C++11 and many of its features. I am looking for a C++11 (non boost) implementation of a thread safe singleton, using lambda and call_once (Sorry... I have no rights to include the call_once tag in the post). I have investigated quite a lot (I am using g++ (4.8, 5.x, 6.2), clang++3.8, Ubuntu 14.04, trying to avoid using boost), and I have found the following links: http://www.nuonsoft.com/blog/2012/10/21/implementing-a-thread-safe-singleton-with-c11/comment-page-1/ http://silviuardelean.ro/2012/06/05/few-singleton-approaches/ (which seems to be very similar to the previous one, but it is more complete, and provides at the end its own implementation). But: I am facing these problems with the mentioned implementations: Or I am writing a wrong implementation of main function (probable), or there are mistakes in the posted codes (less probable), but I am receiving different compiling / linking errors (or both things at the same time, of course...). Similar happens with following code, which seems to compile according to comments (but this one does not use lambda, neither call_once): How to ensure std::call_once really is only called once (In this case, it compiles fine, but throws the following error in runtime): terminate called after throwing an instance of 'std::system_error' what(): Unknown error -1 Aborted (core dumped) So, could you help me, please, with the correct way to call the getInstance() in the main function, to get one (and only one object) and then, how to call other functions that I might include in the Singleton? (Something like: Singleton::getInstance()->myFx(x, y, z);?

(Note: I have also found several references in StackOverflow, which are resolved as "thread safe", but there are similar implementations in other StackOverflow posts and other Internet places which are not considered "thread safe"; here are a few example of both (these do not use lambda) ):

Thread-safe singleton in C++11 c++ singleton implementation STL thread safe Thread safe singleton in C++ Thread safe singleton implementation in C++ Thread safe lazy construction of a singleton in C++

Finally, I will appreciate very much if you can suggest to me the best books to study about these subjects. Thanks in advance!!

Community
  • 1
  • 1

2 Answers2

1

I just ran across this issue. In my case, I needed to add -lpthread to my compilation options.

Hofstee
  • 11
  • 1
1

Implementing a singleton with a static variable as e. g. suggested by Thread safe singleton implementation in C++ is thread safe with C++11. With C++11 the initialization of static variables is defined to happen on exactly one thread, and no other threads will proceed until that initialization is complete. (I can also backup that with problems we recently had on an embedded platform when we used call_once to implement a singleton and it worked after we returned to the "classic" singleton implementation with the static variable.)

ISO/IEC 14882:2011 defines in §3.6.2 e. g. that

Static initialization shall be performed before any dynamic initialization takes place.

and as part of §6.7:

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.

(See also this answer)

A very good book I can recommend is "C++ Concurrency in Action" by A. Williams. (As part of Chapter 3 call_once and the Singleton pattern is discussed - that is why I know that the "classic Singleton" is thread safe since C++11.)

Sonic78
  • 680
  • 5
  • 19
  • @Pablo Esteban Camacho: thanks for the answer! Actually, this clarifies me about the security of using call_once and the improvement of the tool over the classic solution (mutex). – Carlos Alce Mar 24 '18 at 20:32