Thread-safe singleton pattern
Having considered [ http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt] I propose singleton implementation. I had attached "singleton.hpp" to this letter. Below is the test program: #include <stdio.h> #include <singleton.hpp> class A : public boost::Singleton<A> { // // Only Singleton class can create and delete us! // SINGLETON_IS_MY_FRIEND; public: void foo(){ printf("foo!\n"); } A(){ printf("A()\n"); } ~A(){ printf("~A()\n"); } }; void main() { // It is not compiled: //A a = A::Instance(); // That is exactly as it should be used: A::Instance().foo(); } Test program outputs: A() foo! ~A() Does Boost community think that boost::Singleton is convenient solution?
Hi Andrew. 2010/1/26 Andrew Chinkoff <achinkoff@gmail.com>:
Does Boost community think that boost::Singleton is convenient solution?
After a quick glance at your suggested implementation, I see some issues: 1. "__SINGLETON_HPP__" is reserved 2. the same for "____DOOMED_SINGLETON_MUST_NOT_BE_ARRAY____" 3. use 0 rather than NULL 4. you might want to read http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf (on double checked locking) 5. http://www.boost.org/doc/libs/1_41_0/doc/html/thread/synchronization.html#th... might help you Kind regards Ingolf
participants (2)
-
Andrew Chinkoff
-
Ingolf Steinbach