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

Singleton Class and Performance issues

P: n/a
Hi,

I've one question related to singelton implementation of a class. Suppose a
class is as Singelton and contains a public method. If there are more than
one requests for the same method simultaneously. How the dotnet runtime
handles this scenario.
Will it force the second request to wait the till the execution of first
request for the same method.
Please throw some light.
Regards,
Ajay
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Unless you are using threads you, a method can only have one invocation at a
time.

If you are using threads, you might need to do something like
"lock(this){...}" when in the factory method so that you can ensure that an
object is not partially created when the next tread tries to access it.
"Ajay Pal Singh" <aj***@nagarro.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

I've one question related to singelton implementation of a class. Suppose
a class is as Singelton and contains a public method. If there are more
than one requests for the same method simultaneously. How the dotnet
runtime handles this scenario.
Will it force the second request to wait the till the execution of first
request for the same method.
Please throw some light.
Regards,
Ajay

Nov 17 '05 #2

P: n/a
You need to handle synchronization the same in this singleton class as
you would any other class in a multithreaded environment. Use
lock(this) for instance variables accessed in your method and
lock(typeof(myclass)) for static variable protection.

Also, make sure you're implementing the singleton class correctly.

http://msdn.microsoft.com/library/de...onInCsharp.asp

Nov 17 '05 #3

P: n/a
Multiple threads can call a single method almost "simultaneously" so in
a free
threaded environment you will need to consider if a race condition or
concurrency conflict can occur. Thread local storage and immutable
variables
and objects are generally free of race condition, concurrency conflicts.
So to be
clear, if two threads call the same method "almost simultaneously", both
threads
will get a separate stack frame for local storage.

http://www.geocities.com/Jeff_Louie/OOP/oop25.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4

P: n/a
Ajay Pal Singh <aj***@nagarro.com> wrote:
I've one question related to singelton implementation of a class. Suppose a
class is as Singelton and contains a public method. If there are more than
one requests for the same method simultaneously. How the dotnet runtime
handles this scenario.
Will it force the second request to wait the till the execution of first
request for the same method.


Not by default, no - assuming the methods are being called in different
threads, the same code can be executing in two threads unless you put
some locking in. Depending on your situation, you may need locking or
it may be a terrible idea.

I suggest you read
http://www.pobox.com/~skeet/csharp/singleton.html for how to make the
"singleton" bit of your code threadsafe, and
http://www.pobox.com/~skeet/csharp/threads
for general threading advice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

P: n/a
> I suggest you read
http://www.pobox.com/~skeet/csharp/singleton.html


Jon, I've read above many times and I think it's the best you've ever
written.

Regards
- Michael S
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.