473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

try this once

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
4 1005
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


"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stuart A Yeates | last post by:
I'm trying to write a template that matches "*" but which has an if clause which if only executed once per type of element seen (where all elements of a type have the same name and namespace). The...
1
by: Jacob Jensen | last post by:
Hi I have a problem creating C++ code that is multiplatform compilable. My problem is using the "#pragma once" directive which can be used by microsoft Visual Studio pre-compiler but which gives...
2
by: Just Me | last post by:
When a document is to be printed I call a method that contains an AddHandler statement. I just realized that if a second copy is to be printed the method is called and the AddHandler is executed...
5
by: ShaunO | last post by:
BACKGROUND I have the following classes as part of a program that opens 3 Asynchronous Sockets. Each socket is in a separate instance of a wrapping class. 1x User Interface class 1x Client...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I have been using the Click Once deployment feature of VS2005 - very nice. I had IE6 at the time when I started using Click Once Deployment, and everything worked fine. But then my...
10
by: Lorenzo Di Gregorio | last post by:
Hello, I've been using Python for some DES simulations because we don't need full C speed and it's so much faster for writing models. During coding I find it handy to assign a variable *unless...
29
by: Neil | last post by:
Running a SQL 7 system on a Windows 2000 server using Access 2000 on client machines as a front end. System administrator currently reboots the server once a month. Yesterday we had some weird...
26
by: Rick | last post by:
I'm told that "#pragma once" has made it into the ISO standard for either C or C++. I can't find any reference to that anywhere. If it's true, do any of you have a reference I can use? ...
5
by: raashid bhatt | last post by:
What is #pragma once used for and what is #WIN#@_LEN_AND_MEAN
4
by: Yonih | last post by:
Hey Yall, I am trying to incorporate a Once Per session Cookie that will expire once the browser is closed so some one who comes to my website will see my popup the first time her visits the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.