Boost logo

Boost Users :

Subject: [Boost-users] Thread-safe singleton pattern
From: Andrew Chinkoff (achinkoff_at_[hidden])
Date: 2010-01-25 18:37:00


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?





Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net