Schwartz counters.

Discussion in 'C++' started by Dave Townsend, Jun 26, 2004.

  1. John,

    Schwartz counters are a variation of the Singleton pattern to control the
    lifetime
    of a shared resource such as a file stream such that the resource is created
    only
    once on the first use, and destroyed when the last client of the resource is
    destroyed.
    A mgr class controls the shared resource and keeps track of how many times
    the constructor
    for the resource is called and how many times the destructor is called.
    Here is an example to explain better than my words...

    dave



    #include <iostream>
    #include <string>
    using namespace std;

    class SharedResource
    {
    public:
    SharedResource()
    {
    cout << "Create shared resource\n";

    }
    ~SharedResource()
    {
    cout << "Closing shared resource \n";
    }

    void printmsg( const string& Message )
    {
    cout << "SharedResource::Msg " << Message << "\n";
    }

    };


    class SharedResourceMgr
    {
    public:
    SharedResourceMgr()
    {
    if ( !_count)
    {
    _SharedResource = new SharedResource();
    }
    _count++;
    }
    ~SharedResourceMgr()
    {
    _count--;

    if (!_count )
    {
    delete _SharedResource;
    }
    }
    SharedResource* getSharedResource(){ return _SharedResource;}

    private:
    static long _count;
    static SharedResource* _SharedResource;


    };

    long SharedResourceMgr::_count =0;
    SharedResource* SharedResourceMgr::_SharedResource = 0;


    class C1
    {
    public:
    SharedResourceMgr _foo;
    void printmsg( const string& msg){
    _foo.getSharedResource()->printmsg(msg);}

    };
    class C2
    {
    public:
    SharedResourceMgr _foo;
    void printmsg( const string& msg){
    _foo.getSharedResource()->printmsg(msg); };
    };
    class C3
    {
    public:
    SharedResourceMgr _foo;
    void printmsg( const string& msg){
    _foo.getSharedResource()->printmsg(msg); };
    };

    // static instances
    C1 c1;
    C1 c2;
    C1 c3;

    int main(int argc, char* argv[])
    {
    c1.printmsg("C1");
    c2.printmsg("C2");
    c3.printmsg("C3");


    return 0;
    }
     
    Dave Townsend, Jun 26, 2004
    #1
    1. Advertising

  2. "Dave Townsend" <> wrote in message
    news:...
    > John,
    >
    > Schwartz counters are a variation of the Singleton pattern to control the
    > lifetime
    > of a shared resource such as a file stream such that the resource is

    created
    > only
    > once on the first use, and destroyed when the last client of the resource

    is
    > destroyed.
    > A mgr class controls the shared resource and keeps track of how many times
    > the constructor
    > for the resource is called and how many times the destructor is called.
    > Here is an example to explain better than my words...
    >
    > dave
    >


    Thanks for the clear explanation. I was hoping for something a bit more
    sophisticated though. The original quote made it sound like it might be
    something for controlling the order of initialisation across translation
    units.

    John
     
    John Harrison, Jun 27, 2004
    #2
    1. Advertising

  3. * John Harrison:
    >
    > "Dave Townsend" <> wrote in message
    > news:...
    > > John,
    > >
    > > Schwartz counters are a variation of the Singleton pattern to control the
    > > lifetime
    > > of a shared resource such as a file stream such that the resource is

    > created
    > > only
    > > once on the first use, and destroyed when the last client of the resource

    > is
    > > destroyed.
    > > A mgr class controls the shared resource and keeps track of how many times
    > > the constructor
    > > for the resource is called and how many times the destructor is called.
    > > Here is an example to explain better than my words...
    > >
    > > dave
    > >

    >
    > Thanks for the clear explanation. I was hoping for something a bit more
    > sophisticated though. The original quote made it sound like it might be
    > something for controlling the order of initialisation across translation
    > units.


    It is related. I think Andrei covered that usage in Modern C++ Design.
    One problem it can solve is that while static destruction is opposite
    order of construction, one may want e.g. a logging facility to exist
    as long as anyone is using it; it might be instantiated late on, but
    used till the end of the program.

    But I totally agree with you that using a special name for something
    conceptually trivial like that (although its application is not
    necessary so) is in my words a case of projecting an appearance of
    sophistication when the reality is mundane, where that effort by itself
    degrades the communication to the point where little can be understood.

    Henceforth we shall not say "smart-pointer" but "indirection-operator
    based generic implemention of Handle/Body pattern with automatic memory
    reclamation semantics", or perhaps "Kalasabunka-Flirrp handles", after
    the well-known (?) computer scientists Kalasabunka and Flirrp.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jun 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eli Bendersky
    Replies:
    7
    Views:
    628
    Nicolas Matringe
    Oct 27, 2004
  2. John Harrison

    schwartz counters?

    John Harrison, Jun 26, 2004, in forum: C++
    Replies:
    1
    Views:
    1,146
    John Carson
    Jun 27, 2004
  3. Ramon F Herrera
    Replies:
    0
    Views:
    282
    Ramon F Herrera
    May 8, 2008
  4. hudson

    and Randal L. Schwartz the hacker

    hudson, Aug 20, 2003, in forum: Perl Misc
    Replies:
    10
    Views:
    162
    Matt Garrish
    Aug 22, 2003
  5. weston
    Replies:
    5
    Views:
    129
    Dr.Ruud
    Mar 4, 2006
Loading...

Share This Page