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

try this once

P: n/a
public sealed class singelton
{
private static readonly singelton instance;
private static Object syncroot=new Object();
private singelton();
public static singelton instance
{
get
{

if(instance==null)
{
Lock(syncroot)
{
if(instance==null)
instance=new singleton();
}
}return instance;

}
}
}
Jul 14 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
There is a very good discussion of Singleton patterns on Jon's site:
http://www.pobox.com/~skeet/csharp/singleton.html

Note that this double-checked/locked approach is exactly the 3rd
version, which Jon then justifies as "// Bad code! Do not use!"

Marc
Jul 14 '08 #2

P: n/a


"Marc Gravell" <ma**********@gmail.comwrote in message
news:uT**************@TK2MSFTNGP03.phx.gbl...
There is a very good discussion of Singleton patterns on Jon's site:
http://www.pobox.com/~skeet/csharp/singleton.html

Note that this double-checked/locked approach is exactly the 3rd version,
which Jon then justifies as "// Bad code! Do not use!"

Marc

One of the comments Jon writes in the 2nd bullet of the 3rd version:

"I tend to try to avoid situations where experts don't agree what's right
and what's wrong!"

Hmm, Jon = expert....does he not agree either/or? Or is he not an expert in
the 'explicit memory barrier' field? :P

Mythran
Jul 14 '08 #3

P: n/a
Or is he not an expert in
the 'explicit memory barrier' field? *:P
I think the key point here is that there are people who truly are
experts in this area (far deeper than language level) who can't agree;
it doesn't help that part of this is theory (the CLR/CLI spec) rather
than practice (the MS .NET CLR implementation), which means you can't
strictly prove it just by example, because that could be an
implementation detail. So since it is so easy to work around the
ambiguity...?

Marc
Jul 14 '08 #4

P: n/a
Mythran <My*****@community.nospamwrote:
One of the comments Jon writes in the 2nd bullet of the 3rd version:

"I tend to try to avoid situations where experts don't agree what's right
and what's wrong!"

Hmm, Jon = expert....does he not agree either/or? Or is he not an expert in
the 'explicit memory barrier' field? :P
I believe that the version with a volatile field is probably okay.
However, I don't regard myself as a real expert in this field (or
virtually any field, to be honest). When people who definitely know
more than I do disagree, it makes life tricky.

I *think* that the volatile version is largely regarded as safe, but
there are lock-free ways of doing it which are more debatable.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.