15.12 The lock statementreleases the lock.

王朝other·作者佚名  2006-05-06
宽屏版  字体: |||超大  

lock ( expression ) embedded-statement

The expression of a lock statement must denote a value of a reference-type.

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.

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]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有