Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 15 Statementsprevious at this levelnext at this level 15.12 The lock statement Paragraph 11 The lock statement obtains the mutual-exclusion lock for a given object, executes a statement, and then releases the lock. lock-statement : lock ( expression ) embedded-statement Paragraph 21 The expression of a lock statement must denote a value of a reference-type. 2 No implicit boxing conversion (§13.1.5) is ever performed for the expression of a lock statement, and thus it is a compile-time error for the expression to denote a value of a value-type. Paragraph 31 A lock statement of the form
lock (x) ...  
where x is an expression of a reference-type, is precisely equivalent to
System.Threading.Monitor.Enter(x);  
try {  
   ...  
}  
finally {  
   System.Threading.Monitor.Exit(x);  
}  
except that x is only evaluated once.
[Example: The System.Type object of a class can conveniently be used as the mutual-exclusion lock for static methods of the class. For example:
class Cache  
{  
   public static void Add(object x) {  
      lock (typeof(Cache)) {  
         ...  
      }  
   }  
   public static void Remove(object x) {  
      lock (typeof(Cache)) {  
         ...  
      }  
   }  
}  
end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS