15

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?

Community
  • 1
  • 1
  • 2
    Can I ask why you want to know? Most people do not attempt to write lock-free multithreaded algorithms in VB. – Eric Lippert Jul 08 '09 at 05:47
  • @EricLippert, excuse me if I sound rude, but why do you ask "why you want to know"? If most people have no wish to reach for the stars - should those few, who try, stop? I, personally, do a lot of development in VB and can say that this language is far ahead of most languages. – Dima Aug 22 '16 at 20:10
  • 2
    @Dima: Because at the time I was on the design team for C# and in daily contact with the VB design team. By soliciting feedback directly from real users with real needs, and determining what those needs are in their context, I could more capably and accurately express those user needs to my colleagues on the design teams for both languages. I'm glad to hear that VB meets your needs; it's a delightful and quirky language. – Eric Lippert Aug 22 '16 at 20:21
  • @EricLippert it's a delightful and quirky language ONLY for newbie. Most programmers used VB since 1996 from VB, than gently moved to VB.NET (since 2002). And any of this programmers (included me) don't understand why MS can not support own proprietary RAPID technology. BTW I always write lock-free multithreaded code in VB. –  Nov 19 '22 at 20:14

3 Answers3

16

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
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I like this, thanks. Do you have any suggestion as to how it might be used with static fields, e.g. like [this](https://www.red-gate.com/simple-talk/dotnet/c-programming/working-with-the-httpclient-class/)? Would a person use these methods when calling members of the `HttpClient`-typed field? – InteXX Jul 03 '18 at 19:07
  • Is this outdated, or is there a reason to use this approach over System.Thread.VolatileRead() and VolatileWrite()? – Max Barraclough Sep 05 '18 at 15:36
14

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

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

Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
  • 1
    Everyone should now be using System.Threading.Volatile object instead, see https://stackoverflow.com/questions/15039188/volatile-vs-volatileread-write – Michael Erickson Nov 30 '18 at 23:24
2

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...

Peter
  • 37,042
  • 39
  • 142
  • 198