Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple C++ logger by using singleton pattern

Due to the flooding examples of implementing logger using Singleton pattern, I have just written a simple C++ logger in the same approach for my program. However, since the famous double-checked locking approach is known to be no more thread-safe, I wonder if I should:

1) Forget about the use of Singleton pattern in this case?

2) Continue to use double-checked locking even though it is unsafe?

3) Use the expensive pure sync lock method for every access to its public interfaces?

Any suggestions?

like image 895
shiouming Avatar asked Nov 09 '09 14:11

shiouming


2 Answers

Use Meyers Singleton. If you are using using gcc at least initialization is thread-safe.

class Singleton{
   Singleton(){
    //This is threadsafe in gcc, no mutex required
   }
   static Singleton * instance(){
      static Singleton myinstance;
      return &myinstance;
   }
};

gcc guards static locals construction unless you disable with -fno-threadsafe-statics, I recently wrote about that here

like image 177
Arkaitz Jimenez Avatar answered Sep 21 '22 10:09

Arkaitz Jimenez


In applications with threads, I prefer to use singletons with an initialize() function and asserts to make sure that the initialize() is used before the first instance(). Call initialize() from the main thread. I don't think that lazy instantiation is really the key feature of a singleton, especially for a logger.

While Arkaitz's answer is more elegant, my answers avoids threading issues on all platforms with the cost of 1 extra function and some instantiating concerns during startup for singletons with dependencees (helped by asserts and ofcourse: use singletons judiciously).

like image 31
stefaanv Avatar answered Sep 21 '22 10:09

stefaanv