C#“volatile”的等效 VB.NET 关键字是什么?
如果没有关键字,什么机制是等效的?
VB.NET 中没有与 C# 的 volatile 关键字等效的关键字。C# 中的 Volatile 只是确保编译器在生成 IL 时以不同方式处理事情,但 VB.NET 编译器没有此选项。
您可以通过这种方式解决它(取自此博客文章):
Function VolatileRead(Of T)(ByRef Address As T) As T
VolatileRead = Address
Threading.Thread.MemoryBarrier()
End Function
Sub VolatileWrite(Of T)(ByRef Address As T, ByVal Value As T)
Threading.Thread.MemoryBarrier()
Address = Value
End Sub
使用Thread.VolatileRead()
和VolatileWrite()
方法来自 BCL。
http://msdn.microsoft.com/en-us/library/system.threading.thread.volatileread.aspx
根据您使用的变量类型,我建议使用
System.Threading.Thread.VolatileRead()
System.Threading.Thread.VolatileWrite()
System.Threading.Interlocked 也包含一些不错的东西......