日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Csharp volatile 关键字

發(fā)布時(shí)間:2025/6/17 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Csharp volatile 关键字 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

volatile 關(guān)鍵字指示一個(gè)字段可以由多個(gè)同時(shí)執(zhí)行的線程修改。聲明為 volatile 的字段不受編譯器優(yōu)化(假定由單個(gè)線程訪問(wèn))的限制。這樣可以確保該字段在任何時(shí)間呈現(xiàn)的都是最新的值。

volatile 修飾符通常用于由多個(gè)線程訪問(wèn)但不使用 lock 語(yǔ)句對(duì)訪問(wèn)進(jìn)行序列化的字段。

volatile 關(guān)鍵字可應(yīng)用于以下類型的字段:

  • 引用類型。

  • 指針類型(在不安全的上下文中)。請(qǐng)注意,雖然指針本身可以是可變的,但是它指向的對(duì)象不能是可變的。換句話說(shuō),您無(wú)法聲明“指向可變對(duì)象的指針”。

  • 整型,如 sbyte、byte、short、ushort、int、uint、char、float 和 bool。

  • 具有整數(shù)基類型的枚舉類型。

  • 已知為引用類型的泛型類型參數(shù)。

  • 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

轉(zhuǎn)載于:https://www.cnblogs.com/fengye87626/p/3835006.html

總結(jié)

以上是生活随笔為你收集整理的Csharp volatile 关键字的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。