ݺߣ

ݺߣShare a Scribd company logo
JR C++ constructorSingletonՄ


             Luba Tang
             2010/5/11
ʲN singleton?


? ϵy Ψһ  
 C Ψһ
 C 

 
 1. ʽYώ embeded dbm
 2. еP
 3. Register File
y C ȫ׃


P
? P header file ȫ׃
 Chess Board[20][20];


?  implementation file ⲿ׃ʹ
 extern Chess Board[20][20];
/*
 * board.h                               *
 */
class BoardType
{
};
static BoardType Board;


/*
 * user.h                                *
 */
#include board.h
extern BoardType Board;
ȫ׃ singleton aĆ}

? ڶͬr_l֮£cΨһԲ
  ̫ױ

/*
 * user.h                                *
 */
#include board.h
extern BoardType Board;
BoardType MyBoard = Board;
MyBoard->setChess( black, 20 );
ȫ׃ singleton aĆ}


? Ĵ_̈́eһ†?

// lib1.c                             // lib2.c
unsigned int a;                       char a;

// main.c
extern a;              l?


[user@locahost]$ gcc Cc ./lib1.c && gcc Cc ./lib2.c && gcc Cc ./main.c
[user@locahost]$ gcc ./lib1.o ./lib2.o ./main.o
Linker  symbol 

? Linker Ѓɂ rules  symbol
   C ܴڃɂ̖
   C ̖ > ̖
   C g > Сg

?  gcc C compiler 
   C гʼֵ global variable 鏊̖
   C ]гʼֵ global variable ̖ COMMON variable̎


// lib1.c                       // lib2.c
unsigned int a;                 char a;

// main.c
extern a;           unsigned int
Global variable ȱc


1. C CΨһ
 C S copy, build, delete
2. yƾSo
 C ʹõPҪ֪Pe
   һPe޸ģͱȫ޸
C++ 
                                      ṩΨһMc
class Board                      instance ] new ^ new һ
{                               rtͰ֮ǰ new ^ȥ
public:
        static Board* self() {
                if( m_pInstance == 0 )
                       m_pInstance = new Board();
                return m_pInstance;
                                               
        }                              Ҫ staticԱC
private:                               Sضʧ
        Board();
        static Board *m_pInstance;               ǰ
};                                       ҪڌnУǰ
                                      instanceʼָ˿g
Board *Board::m_pInstance = 0;
Compiler  singleton


?C++ static member variable
  C oՓЛ]гʼֵҕ гʼֵ
    global variable鏊̖
  C ̖ linking view ϵΨһ
  C ̖ linking view ϵĴ
}


? c
 C ֪PeBoard::self() Ϳ
   ȡP
? ȱc
 C ҂ֻC linking viewδC run-
   time behavior
 C δC
 delete Board::self(); // Ϸ
C++  (2)

class Board
{
public:
                                  Ҫʽ private
      static Board* self(); Compiler Ԅӎe@ɂ
private:                    member function  public section
                            Ƽ뵽 private section
      Board();
      ~Board();
private:
      static Singleton *m_pInstance;
}
}


? Դ_ˆ?
 C YESo deletenew 
? ΨһԴ_ˆ?
 C NoS copy assignment  copy
   constructor
 C Board b( *Board::self() ); // Ϸ҂Ҫ
C++  (3)
class Board
{
public:                            Ҫʽ private
                             Compiler Ԅӎe@Ă
      static Board* self();  member function  public section
                             Ƽ뵽 private section
private:
      Board();
      ~Board();
      Board( const Board& );
      Board& operator=( const Board& );
private:
      static Singleton *m_pInstance;
}
֪R C static local variable


int Fun()
{
  static int x = 100;
                                ʹ static
  return ++x;           Z:
                         function level ȫ׃
}                       ͬĈgͬӵӛw
                        gͬӵ׃
Meyers Singleton
                                  ṩΨһMc
class Board                static local variable ʾֻڈеһε
{                          rʼஔ֮ǰ if-else Z
public:
        static Board* self() {
                static Board Instance;
                return &Instance;
        }                                        
private:                         static function ԣֻںʽ
        Board();                еĕrŕ׃
        ~Board();
        Board( const Board& );                      ǰ
        Board& operator=( const Board& );    Ƴǰ棬ʽaǬǬQ
};                                           Q
See Again C Meyers Singleton

class Board
{
public:
        static Board* self() {
                static Board Instance;
                return &Instance;
        }
private:
        Board();
        ~Board();
        Board( const Board& );
        Board& operator=( const Board& );
};
Compiler  Static local variable


? normal local variable Q auto-variable
  stack or register 
  C ͨ compiler optimization 
  C Debugger  exception handling Ҫ~
    compensation code ߀ԭ auto-variable

? Static local variable  data/bss section 
  C r² compiler optimization 
     C gcc 
  C ӛwg̶
 compiler ^c Meyers Singleton

class Board
                                            Εrԓ
{                                         constructor?
public:
        static Board* self() {
                static Board Instance;
                return &Instance;      ͵һΈеĕr!
        }                              constructor ^һ
private:                                       function
        Board();
        ~Board();
        Board( const Board& );
        Board& operator=( const Board& );
};
Variable = storage type + Duration

? C++ Storage
   C Static storage
       ?  static  member variable
   C Automatic storage
       ? Auto, register  NOT static, extern 
   C Dynamic storage
       ?  new 
? Storage Duration
   C Automatic duration
       ?  register  auto ׃ scope
   C Dynamic duration
       ?  new _ʼdelete Y
   C Static duration
       ? ijʽ_ʼY
Static storage initialization ĕrC

?  standard f:
    ? Static storage  initialization _ʼ֮ǰҪ zero initialization
    ? Non-local variableՓκ storage typeStatic initialization Ҫ dynamic
      initialization ֮ǰ
         ? Zero initialization
         ? Initialization with constant expression


? Local static variable  initialization ^}s
    C S compiler Լ implementation
    1.  namespace scope _ʼr initialization
    2.  first time control passes through the declaration _ʼræ initialize

? GCC Compiler @:
  C Static storage ֵ[ .data
  C Static storage ]ֵ[ .bss
   C Local static POD (C data type)  1
   C  local static variable  2
See Again C Meyers Singleton

class Board
{
public:
        static Board* self() {
                static Board Instance();
                                      ;     Ŀǰ gcc o^
                return &Instance;
        }
private:
        Board();
        ~Board();
        Board( const Board& );
        Board& operator=( const Board& );
};
†} C Dead Reference Problem

? ϵyYrOӋ singleton 
  пܱܶΣ segmentation
  fault
Dead Reference Problem


? 
 C  singleton  C Board, Game, Log
 C κ singleton l}͕Yϵy
 C  destructor
   ? Log::self()->printf( Dead Mesg );
   ? Clean up member variables
 Singleton Մ constructor
C++ static storageYʽ - FILO


?   Static storage  durationmain֮ǰ main ֮
?   Static storage ȺеYڽĕr
    ͛Qĕrg

    ? Game}
    ? Call Log::self()
       1. Log::Log()
       2. Log::printf()
                               Game::Game()
    ? Throw exception
       ? Log::~Log();
                               Board::~Board()
    ? Board::~Board()
    ? Log::self()->printf();   Segmentation fault
ⷨһ ɜy Dead Reference


? һ static bool m_bDestroy o
  ^
?  Log constructor мДʽД
  񱻴ݚ^
On Dead Singleton (1/2)
class Log
                                                Д pointer  0
{
                                       onDeadReference()  error
public:
                                       create() µ Log 
         static Log* self() {
                  if( m_pInstance == 0 ) {
                           if( m_bDestroy )
                                   onDeadReference();
                           else
                                   create();
                  }
                  return m_pInstance;
         }
private:
         static bool m_bDestory;
         Log* m_pInstance;
On Dead Singleton

private:                                   Meyers Singleton
       static void create() {           ˜ constructor 
               static Log instance;
               m_pInstance = &instance;
       }
       static void onDeadReference() {
                throw std::runtime_error(Dead Occurs);
       }
                                        segmentation fault
       ~Singleton() {               һ exceptionц}
               m_pInstance = 0;     Gȥ
               m_bDestory = true;
       }
};                              ʹ delete
                 ֻҪ pointer O 0 delete
                 m_pInstanceϵyԄ destroy instance
On Dead Singleton (1/2)

class Log                                  Д pointer  0
{                                 onDeadReference()  error
public:                           create() µ Log 
        static Log* self() {
                if( m_pInstance == 0 ) {
                       if( m_bDestroy )
                              onDeadReference();
                       else
                              create();
                }
                return m_pInstance;
        }
                                     Private instance pointerĞ
private:                                         static
        static bool m_bDestory;
        static Log* m_pInstance;
        Log* m_pInstance;
}


? c
 C l segmentation faulte`̎
   ؓ؟̎ exception
? ȱc
 C Ҫ~e`̎e`̎
   dž}lԪ֮һ (籾 Log
   )
 C ]ĽQ}ֻJ֪}
Phoenix Singleton


?    Q
    C ϣ onDeadReference() ܉³
      ʼ Log׌ Log ͻ^m
?    P춏ͻҪļ
    1.   ͬӵӛwλ
    2.   ͬӵӛwС
    3.   ͬǰӛw
    4.   ָΕrȥһ
Y

? Placement Operator new()
  C Z
    new(address) class_name();
  C x
    1.  address cһ class_name 
    2. ȥ] atexit
? atexit
  C Z
    int atexit( void (*pFun)() );
  C x
    ]ԽYrԓһ function, FILO stack
Phoenix Singleton
                                    ȡָ
private:                       create mȻoȡ
       static void             ǿȡ
                onDeadReference() {
                               ָ
               create();
               new(m_pInstance) Log;      ͬλý
               atexit(killLog);           Bظʼ
                                          Boӛ
               m_bDestory = false;
       }                                     ]ʽ
       static void killLog() {         䌍gӺ destructor
               m_pInstance->~Log();     operator new һ
                                       Ҫ atexit
       }
};
                       ʽ
                     gӺdestructor
 Phoenix Singleton (1/3)

class Log
{
public:
        static Log* self() {
                if( m_pInstance == 0 ) {
                       if( m_bDestroy )
                              onDeadReference();
                       else
                              create();
                }
                return m_pInstance;
        }
 Phoenix Singleton (2/3)

private:
       static void onDeadReference() {
               create();
               new(m_pInstance) Log;
               atexit(killLog);
               m_bDestory = false;
       }
       static void killLog() {
               m_pInstance->~Log();
       }
       static void create() {
               static Log instance;
               m_pInstance = &instance;
       }
 Phoenix Singleton (3/3)

private:
        ~Log() {
                m_pInstance = 0;
                m_bDestory = true;
        }
        // constructor, copy constructor, and assignment
        
private:
        static bool m_bDestory;
        static Log* m_pInstance;
};
// in cpp
bool Log::m_bDestory = false;
Log* Log::m_pInstance = 0;
ܲDzBֻ 32+ ؈


?atexit ֻҪ֧Ԯ ] 32 
 functions

?GCC ü
 ?Libiberty ṩ xatexit]
Singleton ӷ

? c
 C áÌ
 C  global variable ܶలȫ

? ȱc
 C ̫ã׌ӛ䚧PSϵy

? ͵! ҪҎcPS
  ܛQҪҪʹ singleton
? ͨҎ֮ᣬsingleton
MA}


? Thread-safe singleton
  C Is Meyers singleton thread-safed?
  C If not, please implement one
? Template singleton
  C Refer to
    template<typename T> llvm::ManagedStatic
? Longevity singleton

More Related Content

Similar to Singleton Մ constructor (20)

Keep your code clean
Keep your code cleanKeep your code clean
Keep your code clean
macrochen
?
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointers
chchwy Chang
?
C++11//Overview of C++11 New Features
C++11//Overview of C++11 New FeaturesC++11//Overview of C++11 New Features
C++11//Overview of C++11 New Features
Peien Luo
?
а++ʰ
а++ʰа++ʰ
а++ʰ
Ⱥ
?
ϵyܘOӋ Android
ϵyܘOӋ  AndroidϵyܘOӋ  Android
ϵyܘOӋ Android
ԣ
?
Lua Խ
Lua ԽLua Խ
Lua Խ
gowell
?
Raspberry PIWLinuxӳʽ
Raspberry PIWLinuxӳʽRaspberry PIWLinuxӳʽ
Raspberry PIWLinuxӳʽ
Stanley Ho
?
Object-Based Programming Part II
Object-Based Programming Part IIObject-Based Programming Part II
Object-Based Programming Part II
PingLun Liao
?
JR C++11 ˜˜ʼʹ AMP ʽƽ\
JR C++11 ˜˜ʼʹ AMP ʽƽ\JR C++11 ˜˜ʼʹ AMP ʽƽ\
JR C++11 ˜˜ʼʹ AMP ʽƽ\
d
?
ՌW Android n - Part1: Kotlin ZB
ՌW Android n -  Part1: Kotlin ZBՌW Android n -  Part1: Kotlin ZB
ՌW Android n - Part1: Kotlin ZB
Johnny Sung
?
JCConf 2024 - Java 22 & 23 ¹ܽB
JCConf 2024 - Java 22 & 23 ¹ܽB JCConf 2024 - Java 22 & 23 ¹ܽB
JCConf 2024 - Java 22 & 23 ¹ܽB
Joseph Kuo
?
һ򵥵ij̸DD֮
һ򵥵ij̸DD֮һ򵥵ij̸DD֮
һ򵥵ij̸DD֮
yiditushe
?
Java Crash2012-05-10
Java Crash2012-05-10Java Crash2012-05-10
Java Crash2012-05-10
Kris Mok
?
Rootkit 101
Rootkit 101Rootkit 101
Rootkit 101
WEI CHIEH CHAO
?
ʳDzԱ,Dz,𳦳-
ʳDzԱ,Dz,𳦳-ʳDzԱ,Dz,𳦳-
ʳDzԱ,Dz,𳦳-
Lucien Li
?
ʳDzԱ,Dz,𳦳-
ʳDzԱ,Dz,𳦳-ʳDzԱ,Dz,𳦳-
ʳDzԱ,Dz,𳦳-
Lucien Li
?
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code clean
macrochen
?
C++11//Overview of C++11 New Features
C++11//Overview of C++11 New FeaturesC++11//Overview of C++11 New Features
C++11//Overview of C++11 New Features
Peien Luo
?
а++ʰ
а++ʰа++ʰ
а++ʰ
Ⱥ
?
ϵyܘOӋ Android
ϵyܘOӋ  AndroidϵyܘOӋ  Android
ϵyܘOӋ Android
ԣ
?
Raspberry PIWLinuxӳʽ
Raspberry PIWLinuxӳʽRaspberry PIWLinuxӳʽ
Raspberry PIWLinuxӳʽ
Stanley Ho
?
Object-Based Programming Part II
Object-Based Programming Part IIObject-Based Programming Part II
Object-Based Programming Part II
PingLun Liao
?
JR C++11 ˜˜ʼʹ AMP ʽƽ\
JR C++11 ˜˜ʼʹ AMP ʽƽ\JR C++11 ˜˜ʼʹ AMP ʽƽ\
JR C++11 ˜˜ʼʹ AMP ʽƽ\
d
?
ՌW Android n - Part1: Kotlin ZB
ՌW Android n -  Part1: Kotlin ZBՌW Android n -  Part1: Kotlin ZB
ՌW Android n - Part1: Kotlin ZB
Johnny Sung
?
JCConf 2024 - Java 22 & 23 ¹ܽB
JCConf 2024 - Java 22 & 23 ¹ܽB JCConf 2024 - Java 22 & 23 ¹ܽB
JCConf 2024 - Java 22 & 23 ¹ܽB
Joseph Kuo
?
һ򵥵ij̸DD֮
һ򵥵ij̸DD֮һ򵥵ij̸DD֮
һ򵥵ij̸DD֮
yiditushe
?
Java Crash2012-05-10
Java Crash2012-05-10Java Crash2012-05-10
Java Crash2012-05-10
Kris Mok
?
ʳDzԱ,Dz,𳦳-
ʳDzԱ,Dz,𳦳-ʳDzԱ,Dz,𳦳-
ʳDzԱ,Dz,𳦳-
Lucien Li
?
ʳDzԱ,Dz,𳦳-
ʳDzԱ,Dz,𳦳-ʳDzԱ,Dz,𳦳-
ʳDzԱ,Dz,𳦳-
Lucien Li
?

Singleton Մ constructor

  • 1. JR C++ constructorSingletonՄ Luba Tang 2010/5/11
  • 2. ʲN singleton? ? ϵy Ψһ C Ψһ C 1. ʽYώ embeded dbm 2. еP 3. Register File
  • 3. y C ȫ׃ P ? P header file ȫ׃ Chess Board[20][20]; ? implementation file ⲿ׃ʹ extern Chess Board[20][20];
  • 4. /* * board.h * */ class BoardType { }; static BoardType Board; /* * user.h * */ #include board.h extern BoardType Board;
  • 5. ȫ׃ singleton aĆ} ? ڶͬr_l֮£cΨһԲ ̫ױ /* * user.h * */ #include board.h extern BoardType Board; BoardType MyBoard = Board; MyBoard->setChess( black, 20 );
  • 6. ȫ׃ singleton aĆ} ? Ĵ_̈́eһ†? // lib1.c // lib2.c unsigned int a; char a; // main.c extern a; l? [user@locahost]$ gcc Cc ./lib1.c && gcc Cc ./lib2.c && gcc Cc ./main.c [user@locahost]$ gcc ./lib1.o ./lib2.o ./main.o
  • 7. Linker symbol ? Linker Ѓɂ rules symbol C ܴڃɂ̖ C ̖ > ̖ C g > Сg ? gcc C compiler C гʼֵ global variable 鏊̖ C ]гʼֵ global variable ̖ COMMON variable̎ // lib1.c // lib2.c unsigned int a; char a; // main.c extern a; unsigned int
  • 8. Global variable ȱc 1. C CΨһ C S copy, build, delete 2. yƾSo C ʹõPҪ֪Pe һPe޸ģͱȫ޸
  • 9. C++ ṩΨһMc class Board instance ] new ^ new һ { rtͰ֮ǰ new ^ȥ public: static Board* self() { if( m_pInstance == 0 ) m_pInstance = new Board(); return m_pInstance; } Ҫ staticԱC private: Sضʧ Board(); static Board *m_pInstance; ǰ }; ҪڌnУǰ instanceʼָ˿g Board *Board::m_pInstance = 0;
  • 10. Compiler singleton ?C++ static member variable C oՓЛ]гʼֵҕ гʼֵ global variable鏊̖ C ̖ linking view ϵΨһ C ̖ linking view ϵĴ
  • 11. } ? c C ֪PeBoard::self() Ϳ ȡP ? ȱc C ҂ֻC linking viewδC run- time behavior C δC delete Board::self(); // Ϸ
  • 12. C++ (2) class Board { public: Ҫʽ private static Board* self(); Compiler Ԅӎe@ɂ private: member function public section Ƽ뵽 private section Board(); ~Board(); private: static Singleton *m_pInstance; }
  • 13. } ? Դ_ˆ? C YESo deletenew ? ΨһԴ_ˆ? C NoS copy assignment copy constructor C Board b( *Board::self() ); // Ϸ҂Ҫ
  • 14. C++ (3) class Board { public: Ҫʽ private Compiler Ԅӎe@Ă static Board* self(); member function public section Ƽ뵽 private section private: Board(); ~Board(); Board( const Board& ); Board& operator=( const Board& ); private: static Singleton *m_pInstance; }
  • 15. ֪R C static local variable int Fun() { static int x = 100; ʹ static return ++x; Z: function level ȫ׃ } ͬĈgͬӵӛw gͬӵ׃
  • 16. Meyers Singleton ṩΨһMc class Board static local variable ʾֻڈеһε { rʼஔ֮ǰ if-else Z public: static Board* self() { static Board Instance; return &Instance; } private: static function ԣֻںʽ Board(); еĕrŕ׃ ~Board(); Board( const Board& ); ǰ Board& operator=( const Board& ); Ƴǰ棬ʽaǬǬQ }; Q
  • 17. See Again C Meyers Singleton class Board { public: static Board* self() { static Board Instance; return &Instance; } private: Board(); ~Board(); Board( const Board& ); Board& operator=( const Board& ); };
  • 18. Compiler Static local variable ? normal local variable Q auto-variable stack or register C ͨ compiler optimization C Debugger exception handling Ҫ~ compensation code ߀ԭ auto-variable ? Static local variable data/bss section C r² compiler optimization C gcc C ӛwg̶
  • 19. compiler ^c Meyers Singleton class Board Εrԓ { constructor? public: static Board* self() { static Board Instance; return &Instance; ͵һΈеĕr! } constructor ^һ private: function Board(); ~Board(); Board( const Board& ); Board& operator=( const Board& ); };
  • 20. Variable = storage type + Duration ? C++ Storage C Static storage ? static member variable C Automatic storage ? Auto, register NOT static, extern C Dynamic storage ? new ? Storage Duration C Automatic duration ? register auto ׃ scope C Dynamic duration ? new _ʼdelete Y C Static duration ? ijʽ_ʼY
  • 21. Static storage initialization ĕrC ? standard f: ? Static storage initialization _ʼ֮ǰҪ zero initialization ? Non-local variableՓκ storage typeStatic initialization Ҫ dynamic initialization ֮ǰ ? Zero initialization ? Initialization with constant expression ? Local static variable initialization ^}s C S compiler Լ implementation 1. namespace scope _ʼr initialization 2. first time control passes through the declaration _ʼræ initialize ? GCC Compiler @: C Static storage ֵ[ .data C Static storage ]ֵ[ .bss C Local static POD (C data type) 1 C local static variable 2
  • 22. See Again C Meyers Singleton class Board { public: static Board* self() { static Board Instance(); ; Ŀǰ gcc o^ return &Instance; } private: Board(); ~Board(); Board( const Board& ); Board& operator=( const Board& ); };
  • 23. †} C Dead Reference Problem ? ϵyYrOӋ singleton пܱܶΣ segmentation fault
  • 24. Dead Reference Problem ? C singleton C Board, Game, Log C κ singleton l}͕Yϵy C destructor ? Log::self()->printf( Dead Mesg ); ? Clean up member variables
  • 26. C++ static storageYʽ - FILO ? Static storage durationmain֮ǰ main ֮ ? Static storage ȺеYڽĕr ͛Qĕrg ? Game} ? Call Log::self() 1. Log::Log() 2. Log::printf() Game::Game() ? Throw exception ? Log::~Log(); Board::~Board() ? Board::~Board() ? Log::self()->printf(); Segmentation fault
  • 27. ⷨһ ɜy Dead Reference ? һ static bool m_bDestroy o ^ ? Log constructor мДʽД 񱻴ݚ^
  • 28. On Dead Singleton (1/2) class Log Д pointer 0 { onDeadReference() error public: create() µ Log static Log* self() { if( m_pInstance == 0 ) { if( m_bDestroy ) onDeadReference(); else create(); } return m_pInstance; } private: static bool m_bDestory; Log* m_pInstance;
  • 29. On Dead Singleton private: Meyers Singleton static void create() { ˜ constructor static Log instance; m_pInstance = &instance; } static void onDeadReference() { throw std::runtime_error(Dead Occurs); } segmentation fault ~Singleton() { һ exceptionц} m_pInstance = 0; Gȥ m_bDestory = true; } }; ʹ delete ֻҪ pointer O 0 delete m_pInstanceϵyԄ destroy instance
  • 30. On Dead Singleton (1/2) class Log Д pointer 0 { onDeadReference() error public: create() µ Log static Log* self() { if( m_pInstance == 0 ) { if( m_bDestroy ) onDeadReference(); else create(); } return m_pInstance; } Private instance pointerĞ private: static static bool m_bDestory; static Log* m_pInstance; Log* m_pInstance;
  • 31. } ? c C l segmentation faulte`̎ ؓ؟̎ exception ? ȱc C Ҫ~e`̎e`̎ dž}lԪ֮һ (籾 Log ) C ]ĽQ}ֻJ֪}
  • 32. Phoenix Singleton ? Q C ϣ onDeadReference() ܉³ ʼ Log׌ Log ͻ^m ? P춏ͻҪļ 1. ͬӵӛwλ 2. ͬӵӛwС 3. ͬǰӛw 4. ָΕrȥһ
  • 33. Y ? Placement Operator new() C Z new(address) class_name(); C x 1. address cһ class_name 2. ȥ] atexit ? atexit C Z int atexit( void (*pFun)() ); C x ]ԽYrԓһ function, FILO stack
  • 34. Phoenix Singleton ȡָ private: create mȻoȡ static void ǿȡ onDeadReference() { ָ create(); new(m_pInstance) Log; ͬλý atexit(killLog); Bظʼ Boӛ m_bDestory = false; } ]ʽ static void killLog() { 䌍gӺ destructor m_pInstance->~Log(); operator new һ Ҫ atexit } }; ʽ gӺdestructor
  • 35. Phoenix Singleton (1/3) class Log { public: static Log* self() { if( m_pInstance == 0 ) { if( m_bDestroy ) onDeadReference(); else create(); } return m_pInstance; }
  • 36. Phoenix Singleton (2/3) private: static void onDeadReference() { create(); new(m_pInstance) Log; atexit(killLog); m_bDestory = false; } static void killLog() { m_pInstance->~Log(); } static void create() { static Log instance; m_pInstance = &instance; }
  • 37. Phoenix Singleton (3/3) private: ~Log() { m_pInstance = 0; m_bDestory = true; } // constructor, copy constructor, and assignment private: static bool m_bDestory; static Log* m_pInstance; }; // in cpp bool Log::m_bDestory = false; Log* Log::m_pInstance = 0;
  • 38. ܲDzBֻ 32+ ؈ ?atexit ֻҪ֧Ԯ ] 32 functions ?GCC ü ?Libiberty ṩ xatexit]
  • 39. Singleton ӷ ? c C áÌ C global variable ܶలȫ ? ȱc C ̫ã׌ӛ䚧PSϵy ? ͵! ҪҎcPS ܛQҪҪʹ singleton ? ͨҎ֮ᣬsingleton
  • 40. MA} ? Thread-safe singleton C Is Meyers singleton thread-safed? C If not, please implement one ? Template singleton C Refer to template<typename T> llvm::ManagedStatic ? Longevity singleton