Simple C++ logger by using singleton pattern

前端 未结 4 1924
旧巷少年郎
旧巷少年郎 2021-02-09 17:28

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 famo

4条回答
  •  被撕碎了的回忆
    2021-02-09 17:56

    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

提交回复
热议问题