By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Thread-safety and Singleton methods

P: n/a
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:
----
Parameters are passed to a method using a stack. Each thread maintains it's
own stack so that the parameters are not shared among threads.
----

How about reference type variables....are they also passed using a stack?
Reference-types address is stored in the stack and not their values.....so if
their addresses are being passed, how come they are thread-safe?
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Diffident" <Di*******@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
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:
----
Parameters are passed to a method using a stack. Each thread maintains it's own stack so that the parameters are not shared among threads.
----

How about reference type variables....are they also passed using a stack?
Reference-types address is stored in the stack and not their values.....so if their addresses are being passed, how come they are thread-safe?

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.