Simple C++ logger by using singleton pattern

2019-03-20 13:59发布

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?

4条回答
Fickle 薄情
2楼-- · 2019-03-20 14:25

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

查看更多
神经病院院长
3楼-- · 2019-03-20 14:35

One approach would be to make sure that your first access to the logger comes before your app starts a second thread. By accessing the singleton at a time when you KNOW that there isn't any contention, you make sure that subsequent accesses will always find a pre-existing object and you should completely avoid the problem.

查看更多
再贱就再见
4楼-- · 2019-03-20 14:36

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).

查看更多
你好瞎i
5楼-- · 2019-03-20 14:38

You do not really need separate Initialize() function as this will just contaminate your singleton interface. Just get singleton instance

VERIFY(NULL != Logger::Instance()); 

before any other thread has chance to access it.

查看更多
登录 后发表回答