Superhero with Java powers && Gamer && (Sci-Fi & Star Wars geek) && Bulgarian Java User Group Leader && nerds2nerds podcaster && http://java.beer organizer ? this : null
I find this wrong and buggy, if two thread invoke the Instance at the same time there is a chance two variables to be created and in fact two different instances to be received by the two threads.
I am not a C# guy, sorry but usually in Java volatile and the double check locking helps I believe you can do the same in C#. Also I know in C# you have a Lazy<> which should be used ... but this pattern implementation is not good :( :(
Maybe there is some additional static magic that C# do, but I find it a bit smelly :D
I think the correct implementation is
public sealed class Singleton
{
private static readonly Lazy<Singleton>lazy = new Lazy<Singleton>(()=>new Singleton());
public static Singleton Instance
{
get
{
return lazy.Value;
}
}
private Singleton()
{
}
}
P.S. sorry for the wrong formatting (if it is wrong) I use { on the same line... :D:D:D so :D maybe I messed something :D
Superhero with Java powers && Gamer && (Sci-Fi & Star Wars geek) && Bulgarian Java User Group Leader && nerds2nerds podcaster && http://java.beer organizer ? this : null
here you can check the proper implementation of your example
publicclassMySingleton{privatestaticobjectmyLock=newobject();privatestaticvolatileMySingletonmySingleton=null;// 'volatile' is unnecessary in .NET 2.0 and laterprivateMySingleton(){}publicstaticMySingletonGetInstance(){if(mySingleton==null){// 1st checklock(myLock){if(mySingleton==null){// 2nd (double) checkmySingleton=newMySingleton();// In .NET 1.1, write-release semantics are implicitly handled by marking mySingleton with// 'volatile', which inserts the necessary memory barriers between the constructor call// and the write to mySingleton. The barriers created by the lock are not sufficient// because the object is made visible before the lock is released. In .NET 2.0 and later,// the lock is sufficient and 'volatile' is not needed.}}}// In .NET 1.1, the barriers created by the lock are not sufficient because not all threads will// acquire the lock. A fence for read-acquire semantics is needed between the test of mySingleton// (above) and the use of its contents. This fence is automatically inserted because mySingleton is// marked as 'volatile'.// In .NET 2.0 and later, 'volatile' is not required.returnmySingleton;}}
without lazy, az you can see volatile is NOT required now days, but double checked locking is !
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I find this wrong and buggy, if two thread invoke the Instance at the same time there is a chance two variables to be created and in fact two different instances to be received by the two threads.
I am not a C# guy, sorry but usually in Java volatile and the double check locking helps I believe you can do the same in C#. Also I know in C# you have a Lazy<> which should be used ... but this pattern implementation is not good :( :(
Maybe there is some additional static magic that C# do, but I find it a bit smelly :D
I think the correct implementation is
P.S. sorry for the wrong formatting (if it is wrong) I use { on the same line... :D:D:D so :D maybe I messed something :D
oh yes also en.wikipedia.org/wiki/Double-check...
here you can check the proper implementation of your example
without lazy, az you can see volatile is NOT required now days, but double checked locking is !