How to implement process wide singleton pattern for assembly using static

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Wong

    #1

    How to implement process wide singleton pattern for assembly using static

    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(proc ess) for share static variable across AppDomains.

    // __declspec(appd omain) 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.


Working...