views:

856

answers:

3

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

If there is no keyword what mechanism is the equivalent?

+2  A: 

Duplicate:

http://stackoverflow.com/questions/929146/how-do-i-specify-the-equivalent-of-volatile-in-vb-net

SolutionYogi
Ahh yes. The ever present duplicate. I did search for it but couldn't find it. Probably because of the poorly chosen tags on that link.
Actually, I used Google to find that StackOverflow question. :)
SolutionYogi
I used Google too. :)
I also used the StackOverflow built in search.
SolutionYogi
+4  A: 

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
A: 

link textI found this article on the web about not needing it:

http://www.developerfusion.com/article/5184/multithreading-in-vbnet/7/

Tim Hoolihan
Hope this doesn't bump an old thread, but first hit on google for me. Article mentions using locking/monitor, but MemoryBarrier() is a faster method of implementing volatile variables.
TamusJRoyce