I\'m attempting to write a lock-free version of a call queue I use for message passing. This is not for anything serious, just to learn about threading.
I\'m relativ
Use Thread.VolatileRead()
and VolatileWrite()
methods from the BCL.
http://msdn.microsoft.com/en-us/library/system.threading.thread.volatileread.aspx
There's no equivalent of C#'s volatile
keyword in VB.NET. Instead what's often recommended is the use of MemoryBarrier. Helper methods could also be written:
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
Also there's a useful blog post on this subject.
The Mono.Cecil reader code makes the FieldType As RequiredModifierType with the ModifierType as System.Runtime.CompilerServices.IsVolatile.
I am not an expert on this subject so hopefully someone else will correct me if I am wrong. From what I understand, the issue of memory optimizations is presently a theoretical one and not necessarily something that will occur in reality. But having said that, I think that by using the Interlocked API for your memory access (regardless of the MemoryBarrier) you would not be affected.
Unfortunately there is not an equivalent for volatile in VB.NET. It's not decorated with a normal attribute, but is rather a special compiler generated modifier. You would need to use Reflection to emit a type with this kind of field.
Here is a resource I often refer to when I have questions about threading in the .NET framework. It's very long but hopefully you will find it useful.
http://www.yoda.arachsys.com/csharp/threads/printable.shtml
Starting in .NET 4.5, they added two new methods to the BCL to simulate the volatile
keyword: Volatile.Read and Volatile.Write. They should be totally equivalent to reading/writing a volatile
field. You can clearly use them in VB.NET . They are better (where better == faster) than the Thread.VolatileRead
/Thread.VolatileWrite
because they use half fences instead of full fences.
You can also write an attribute for "Volatile" using Thread.VolatileRead() and Thread.VolatileWrite() and make all properties/variables with that attribute like:
<Volatile()>
Protected Property SecondsRemaining as Integer
Wrote this somewhere but can't seem to find it right now...