C# VB.NET 中的可变等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1095623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:05:46  来源:igfitidea点击:

Volatile equivalent in VB.NET

c#vb.netmultithreadingvolatile

提问by

Possible Duplicate:
How do I specify the equivalent of volatile in VB.net?

可能的重复:
如何在 VB.net 中指定 volatile 的等效项?

What is the VB.NET keyword equivalent of C# "volatile"?

与 C#“volatile”等效的 VB.NET 关键字是什么?

If there is no keyword what mechanism is the equivalent?

如果没有关键字,什么机制是等效的?

采纳答案by Reed Copsey

There is no equivelent to C#'s volatile keywword in VB.NET. Volatile in C# just makes sure the compiler handles things differently when generating the IL, but the VB.NET compiler does not have this option.

VB.NET 中没有等同于 C# 的 volatile 关键字。C# 中的 Volatile 只是确保编译器在生成 IL 时以不同的方式处理事情,但 VB.NET 编译器没有这个选项。

You can work around it this way (taken from this blog post):

您可以通过这种方式解决它(取自这篇博文):

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

回答by Mike Chamberlain

Use Thread.VolatileRead()and VolatileWrite()methods from the BCL.

使用Thread.VolatileRead()VolatileWrite()方法来自 BCL。

http://msdn.microsoft.com/en-us/library/system.threading.thread.volatileread.aspx

http://msdn.microsoft.com/en-us/library/system.threading.thread.volatileread.aspx

回答by Peter

Depending on what variable type you use i would suggest using

根据您使用的变量类型,我建议使用

System.Threading.Thread.VolatileRead()

System.Threading.Thread.VolatileRead()

System.Threading.Thread.VolatileWrite()

System.Threading.Thread.VolatileWrite()

Also System.Threading.Interlocked contains some nice stuff...

System.Threading.Interlocked 也包含一些不错的东西......