Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using SyncLock
Message
From
24/01/2011 13:01:08
 
 
To
24/01/2011 12:06:19
General information
Forum:
ASP.NET
Category:
Other
Title:
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01496120
Message ID:
01497224
Views:
51
>>This looks like the 'super-safe' C# implementation:
public sealed class Singleton
>>{
>>   private static volatile Singleton instance;
>>   private static object syncRoot = new Object();
>>
>>   private Singleton() {}
>>
>>   public static Singleton Instance
>>   {
>>      get 
>>      {
>>         if (instance == null) 
>>         {
>>            lock (syncRoot) 
>>            {
>>               if (instance == null) 
>>                  instance = new Singleton();
>>            }
>>         }
>>
>>         return instance;
>>      }
>>   }
>>}
(from http://msdn.microsoft.com/en-us/library/ff650316.aspx). Only problem is that VB doesn't have 'volatile' :-{

>
>This "volatile" keyword is a new one for me (been around awhile, but I guess I hadn't heard of it). The link you provided to MSDN states that if your member is declared using the volatile keyword, then you don't have to use a lock ... so, if that's the case, then I'm confused why you did ... guess I should probably read up on it more at some point.

I think it's mainly to prevent the compiler pulling a fast one in the way of optimisation:
http://msdn.microsoft.com/en-us/library/aa645755(v=vs.71).aspx
Previous
Reply
Map
View

Click here to load this message in the networking platform