Csharp volatile 关键字
volatile 關鍵字指示一個字段可以由多個同時執行的線程修改。聲明為 volatile 的字段不受編譯器優化(假定由單個線程訪問)的限制。這樣可以確保該字段在任何時間呈現的都是最新的值。
volatile 修飾符通常用于由多個線程訪問但不使用 lock 語句對訪問進行序列化的字段。
volatile 關鍵字可應用于以下類型的字段:
-
引用類型。
-
指針類型(在不安全的上下文中)。請注意,雖然指針本身可以是可變的,但是它指向的對象不能是可變的。換句話說,您無法聲明“指向可變對象的指針”。
-
整型,如 sbyte、byte、short、ushort、int、uint、char、float 和 bool。
-
具有整數基類型的枚舉類型。
-
已知為引用類型的泛型類型參數。
-
IntPtr 和 UIntPtr。
?
?
?
The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.
The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access.
The volatile keyword can be applied to fields of these types:
-
Reference types.
-
Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile."
-
Types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.
-
An enum type with one of the following base types: byte, sbyte, short, ushort, int, or uint.
-
Generic type parameters known to be reference types.
-
IntPtr and UIntPtr.
The volatile keyword can only be applied to fields of a class or struct. Local variables cannot be declared volatile.
?
?
http://msdn.microsoft.com/en-us/library/vstudio/x13ttww7(v=vs.100).aspx
轉載于:https://www.cnblogs.com/fengye87626/p/3835006.html
總結
以上是生活随笔為你收集整理的Csharp volatile 关键字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS UIView的一些方法和属性(
- 下一篇: unity3d与eclipse协同工作环