' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
Imports System
Imports System.Threading

Namespace Singleton
	Public Class MemBarrierSingleton
		Private Shared m_instance As MemBarrierSingleton
		Private Shared ReadOnly mylock As New Object()
		Private someState As Integer
		Private Sub New()
			someState = 123
		End Sub
		Public Shared ReadOnly Property Instance() As MemBarrierSingleton
			Get
				If m_instance Is Nothing Then
					SyncLock mylock
						If m_instance Is Nothing Then
						    Dim temp As MemBarrierSingleton = New MemBarrierSingleton()
						    Thread.MemoryBarrier()
							m_instance = temp
						End If
					End SyncLock
				End If
				Return m_instance
			End Get
		End Property
		Public Sub SomeMethod()
			Console.WriteLine(someState)
		End Sub
	End Class
End Namespace
