473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Schwartz counters.

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;
}
Jul 22 '05 #1
2 1390

"Dave Townsend" <da********@comcast.net> wrote in message
news:NM********************@comcast.com...
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
Jul 22 '05 #2
* John Harrison:

"Dave Townsend" <da********@comcast.net> wrote in message
news:NM********************@comcast.com...
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?
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Mitchell | last post by:
Hello group, I'm trying to write code to test an application we're developing. This application needs to store its own performance data in the registry, and we'd like to be storing quite a...
1
by: John Harrison | last post by:
In a review of C++ Gotchas by Stephen Dewhurst I read the following "however C++ Gotchas does contain some information that I've not seen elsewhere - the use of translation-unit-local static...
2
by: Don | last post by:
Hello! I am trying to write some code to access performance counters on a remote machine. Ideally, I would like to add a machine to a tree control (like the "Servers" view in the .NET IDE) and...
4
by: Chad Myers | last post by:
I'm instrumenting my app with a few performance counters and I'd like to ask you all for some advice on how to handle performance counter instances. I have a class library that is a base library...
0
by: Christopher Attard | last post by:
Hi, I need to create a dialog like the 'Add Counters' dialog box in perfmon. I'm using the System.Diagnostics namespace class in .NET and I've managed to do it. The problem arises when I'm...
1
by: db | last post by:
Hello, I am writing a utility to get the performance counters from a remote machine. Currently, I am usign the System.Diagnostics.PerformanceCounter class to retrieve these counters, but they...
7
by: James | last post by:
Hi Has anybody had any experience of ASP.Net performance counters not updating. In the performance monitor application when I try to add the groups ASP.NET and ASP.NET Applications the...
0
by: JackC | last post by:
Hi, We have an ASP.NET application with a (kinda shady) COM component that we don't know much about and don't have responsability for. But you know how it is - if the app. goes down its my fault...
10
by: Zytan | last post by:
I made a program using the Timer class, and I start the timer with Timer.Start(), but don't stop it with Timer.Stop(), and I assumed this was ok, but, Process Explorer informs me, after running it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.