VB.NET 中的 挥发性等效物

发布于 2024-07-27 05:32:38 字数 274 浏览 2 评论 0原文

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

相当于 C#“挥发性”的 VB.NET 关键字是什么?

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

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

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

If there is no keyword what mechanism is the equivalent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌枕肩 2024-08-03 05:32:38

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

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.

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
可遇━不可求 2024-08-03 05:32:38

使用 BCL 中的 Thread.VolatileRead() 和 VolatileWrite() 方法。

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

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

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

女皇必胜 2024-08-03 05:32:38

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

System.Threading.Thread.VolatileRead()

System.Threading.Thread.VolatileWrite()

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

Depending on what variable type you use i would suggest using

System.Threading.Thread.VolatileRead()

System.Threading.Thread.VolatileWrite()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文