Volatile 類別

定義

包含用於執行動態記憶體作業的方法。Contains methods for performing volatile memory operations.

public ref class Volatile abstract sealed
public static class Volatile
type Volatile = class
Public Class Volatile
繼承
Volatile

備註

在多處理器系統上,volatile 寫入作業可確保所有處理器都會立即看到寫入記憶體位置的值。On a multiprocessor system, a volatile write operation ensures that a value written to a memory location is immediately visible to all processors. 「變動性讀取」作業會取得由任何處理器寫入記憶體位置的最新值。A volatile read operation obtains the very latest value written to a memory location by any processor. 這些作業可能需要排清處理器快取,這可能會影響效能。These operations might require flushing processor caches, which can affect performance.

注意

在單處理器系統上,volatile 讀取和寫入可確保值是讀取或寫入記憶體中,而不是快取(例如,在處理器暫存器中)。On a uniprocessor system, volatile reads and writes ensure that a value is read or written to memory and not cached (for example, in a processor register). 因此,您可以使用這些作業來同步處理對可由另一個執行緒或硬體更新之欄位的存取。Thus, you can use these operations to synchronize access to a field that can be updated by another thread or by hardware.

動態記憶體作業適用于特殊情況的同步處理,其中一般鎖定不是可接受的替代方法。Volatile memory operations are for special cases of synchronization, where normal locking is not an acceptable alternative. 在正常情況下, C# lock 語句、Visual Basic SyncLock 語句和 Monitor 類別提供同步存取資料的最簡單且最不容易出錯的方式,而 Lazy<T> 類別提供簡單的方式來寫入延遲初始化程式碼,而不需要直接使用重複檢查的鎖定。Under normal circumstances, the C# lock statement, the Visual Basic SyncLock statement, and the Monitor class provide the easiest and least error-prone way of synchronizing access to data, and the Lazy<T> class provides a simple way to write lazy initialization code without directly using double-checked locking.

Volatile.ReadVolatile.Write 方法會啟用語言不支援的功能。The Volatile.Read and Volatile.Write methods enable functionality that is not supported in languages. 例如:For example:

  • 某些語言(例如 Visual Basic)無法辨識動態記憶體作業的概念。Some languages, such as Visual Basic, do not recognize the concept of volatile memory operations. Volatile 類別提供這種語言的功能。The Volatile class provides that functionality in such languages.

    注意

    呼叫其中一個方法只會影響單一記憶體的存取。Calling one of these methods affects only a single memory access. 若要提供有效的欄位同步處理,欄位的所有存取都必須使用 Volatile.ReadVolatile.WriteTo provide effective synchronization for a field, all access to the field must use Volatile.Read and Volatile.Write.

  • 在C#中,在欄位上使用 volatile 修飾詞可保證該欄位的每個存取都是動態記憶體作業,但是 volatile 修飾詞不能套用至陣列元素。In C#, using the volatile modifier on a field guarantees that every access to that field is a volatile memory operation, but the volatile modifier cannot be applied to array elements. Volatile.ReadVolatile.Write 方法可以用於陣列元素。The Volatile.Read and Volatile.Write methods can be used on array elements.

方法

Read(Boolean)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(Byte)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(Double)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(Int16)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(Int32)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(Int64)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(IntPtr)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(SByte)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(Single)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(UInt16)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(UInt32)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(UInt64)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read(UIntPtr)

讀取指定之欄位的值。Reads the value of the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Read<T>(T)

從指定的欄位讀取物件參考。Reads the object reference from the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的後面,則處理器無法將它移至此方法的前面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.

Write(Boolean, Boolean)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(Byte, Byte)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(Double, Double)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(Int16, Int16)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(Int32, Int32)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(Int64, Int64)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(IntPtr, IntPtr)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(SByte, SByte)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(Single, Single)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(UInt16, UInt16)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(UInt32, UInt32)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(UInt64, UInt64)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write(UIntPtr, UIntPtr)

將指定的值寫入指定的欄位。Writes the specified value to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

Write<T>(T, T)

將指定的物件參考寫入指定的欄位。Writes the specified object reference to the specified field. 在需要它的系統上,插入記憶體屏障,防止處理器重新排序記憶體作業,如下所示:如果在程式碼中,讀取或寫入出現在這個方法的前面,則處理器無法將它移至此方法的後面。On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.

適用於

另請參閱