Mutex Constructor (Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Initializes a new instance of the Mutex class with a Boolean value that indicates whether the calling thread should have initial ownership of the mutex.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Public Sub New (
	initiallyOwned As Boolean


Type: System.Boolean

true to give the calling thread initial ownership of the mutex; otherwise, false.

The following code example shows how a local Mutex object is used to synchronize access to a protected resource. The thread that creates the Mutex owns it initially.

Imports System.Threading

Class Test
    ' Create a new Mutex. The creating thread owns the
    ' Mutex.
    Private Shared mut As New Mutex(True)
    Private Const numIterations As Integer = 1
    Private Const numThreads As Integer = 3

    <MTAThread> _
    Shared Sub Main()
        ' Create the threads that will use the protected resource.
        Dim i As Integer
        For i = 1 To numThreads
            Dim myThread As New Thread(AddressOf MyThreadProc)
            myThread.Name = [String].Format("Thread{0}", i)
        Next i

        ' Wait one second before allowing other threads to
        ' acquire the Mutex.
        Console.WriteLine("Creating thread owns the Mutex.")

        Console.WriteLine("Creating thread releases the Mutex." & vbCrLf)
    End Sub 'Main

    Private Shared Sub MyThreadProc()
        Dim i As Integer
        For i = 1 To numIterations
        Next i
    End Sub 'MyThreadProc

    ' This method represents a resource that must be synchronized
    ' so that only one thread at a time can enter.
    Private Shared Sub UseResource()
        ' Wait until it is safe to enter.

        Console.WriteLine("{0} has entered protected area", _

        ' Place code to access non-reentrant resources here.

        ' Simulate some work

        Console.WriteLine("{0} is leaving protected area" & vbCrLf, _

        ' Release Mutex.
    End Sub 'UseResource
End Class 'MyMainClass
' The example displays output like the following:
'       Creating thread owns the Mutex.
'       Creating thread releases the Mutex.
'       Thread1 has entered the protected area
'       Thread1 is leaving the protected area
'       Thread2 has entered the protected area
'       Thread2 is leaving the protected area
'       Thread3 has entered the protected area
'       Thread3 is leaving the protected area

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top