cxlove / DesignPattern

The example code from head first design patterns for practicing
0 stars 2 forks source link

The Singleton Pattern using c++ #2

Open cxlove opened 8 years ago

cxlove commented 8 years ago

RT

Justme0 commented 8 years ago

Using C++11 style: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern 用new的话,不是线程安全的。

cxlove commented 8 years ago

@Justme0 thanks for your reminding. the c++ code just for practicing, I will update it soon. Thanks

cxlove commented 8 years ago

Ref: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern

we used a static variable in a static function. it's thread safety in c++11, you can check it in http://stackoverflow.com/questions/1661529/is-meyers-implementation-of-singleton-pattern-thread-safe.

about the static variable's lift time, you can check it in http://stackoverflow.com/questions/246564/what-is-the-lifetime-of-a-static-variable-in-a-c-function.

cxlove commented 8 years ago

You'll find a discussion of how to implement a thread-safety singleton using c++ in this paper: http://www.aristeia.com/Papers/DDJ%5FJul%5FAug%5F2004%5Frevised.pdf