Thread-safety and Singleton methods

G

Guest

Guys,

I have been cracking my head over this concept in .NET framework. I have
read many posts on this topic but not clear about this and hence I am posting
it again.

If you have designed your class based on singleton pattern where ONLY ONE
instance of class exists for the WHOLE APPLICATION DOMAIN....how can the
public methods in that class be thread-safe? I have read thru posts where
they say that singleton class methods are thread-safe...I request you to
justify this conclusion.

How about static methods in singleton class .... are they thread-safe?

Scott Allen informed in one of his posts that:
 
K

Karl Seguin

This might be useful:

http://odetocode.com/Articles/314.aspx

Reference types, as you say, have their address passed on the stack but
their actual data remains in the heap. However, I don't see this really
being a problem:

Why wouldn't static methods be thread-safe? they apply the same as any
other static method...if you are using static fields (such as the internal
instance of the singleton class) then of course if you change it once it'll
change it everywhere...but if that's a concern, perhaps you aren't using a
singleton properly?

I'm quite confused by your confusion. A static method isn't impacted by the
presence of a singleton...a static function isn't called on an instance of
the class (even if you jsut have one). If the static function is messing
around with the internal static instance,well that' sjust plain silly:

public class SomeClass
private static SomeClass _instance = new SomeClass();
public static SomeClass Instance{
get { return _instance}
}

public static void ScrewMeOver(){
//if you mess with the _instance field in here, you better lock
access
}
}

perhaps you could provide an example which you think would cause problems?

Karl
 

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

Similar Threads

Singleton_pattern and Thread Safety 101
deque and thread-safety 0
Thread Safety? 2
Thread safety 0
boolean and thread safety 19
Thread safe Singleton class 4
Singleton Pattern 7
static statements and thread safety 0

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top