How to implement process wide singleton pattern for assembly using static

T

Tony Wong

I am trying to implement the Singleton Pattern for a assembly (class) which
control a common resource on my computer. I need the Singleton behavior
within a single process which contain multiple AppDomains. I use a static
member to count class instance like this,

public __gc class foo: public IDisposable

{

public:

foo();

~foo();

//! In VS .NET 2003 global variables and static members of native types
are

// shared across AppDomains.

// With VS .NET 2005, you have the option to share or not share
variables

// across AppDomains.

// __declspec(process) for share static variable across AppDomains.

// __declspec(appdomain) have one instance of static variable per
AppDomain.

static Int32 m_refCount = 0;

};

As per my comment in the code, I thought static members are shared across
AppDomain in VS.NET 2003, But when I run my code in the debugger I see a
new instance of the m_refCount in different instances of the class created
in different AppDomains.

Is static members shared across multiple AppDomains within the same process
under VS.NET 2003 or am I seeing a bug in the IDE Debugger (I am using
VS.NET 2003)?

If static members are not shared across multiple AppDomains within the same
process then how does one implement the singleton pattern under managed C++?

Thanks in advance for your help.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top