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

前端 未结 6 1959
误落风尘
误落风尘 2020-12-03 19:05

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

6条回答
  •  無奈伤痛
    2020-12-03 19:25

    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.

提交回复
热议问题