Visual Basic Thread Lock

In visual basic, SyncLock keyword is useful to acquire the mutual-exclusion of lock for the specified block of code to make sure that at a time only one thread can execute it.

 

In case, if any other thread wants to execute the same piece of code, then it should wait until the thread that holds the lock finishes its execution.

 

Generally, the SyncLock keyword is useful while working with multithreading applications to make sure that at a time only thread can access the particular piece of code.

Visual Basic Lock Syntax

Following is the syntax of using SyncLock keyword in visual basic to acquire the mutual-exclusion of lock for the specified piece of code.

 

SyncLock x

// Your code

End SyncLock 

Here, x is an expression of reference type and within the SyncLock statement, we need to write the code which we want to allow access for one thread at a time.

Visual Basic Thread Lock Example

Following is the example of using SyncLock keyword in visual basic to lock the particular piece of code to allow execution of one thread at a time.

 

Imports System.Threading

 

Module Module1

    ReadOnly pblock As Object = New Object()

    Private Sub PrintInfo()

        SyncLock pblock

            For i As Integer = 1 To 4

                Console.WriteLine("i value: {0}", i)

                Thread.Sleep(1000)

            Next

        End SyncLock

    End Sub

    Sub Main(ByVal args As String())

        Dim t1 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))

        Dim t2 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))

        t1.Start()

        t2.Start()

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created multiple threads (t1 & t2) and starting to execute the PrintInfo method simultaneously. Here, in PrintInfo method we used lock statement to acquire the mutual-exclusion of lock for the For loop code execution to allow only one thread execution at a time.

 

When we execute the above result, we will get the result like as shown below.

 

i value: 1

i value: 2

i value: 3

i value: 4

i value: 1

i value: 2

i value: 3

i value: 4

If you observe the result, even if we start the multiple threads simultaneously to execute the same functionality, the SyncLock statement made the second thread to wait until the first thread finishes its execution.

Visual Basic Thread without Lock Example

In the above example, in case, if we didn’t use the SyncLock statement, then the multiple threads will execute simultaneously.

 

Following is the example of multipli threading in vb without using SyncLock statement.

 

Imports System.Threading

 

Module Module1

    ReadOnly pblock As Object = New Object()

    Private Sub PrintInfo()

        For i As Integer = 1 To 4

            Console.WriteLine("i value: {0}", i)

            Thread.Sleep(1000)

        Next

    End Sub

    Sub Main(ByVal args As String())

        Dim t1 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))

        Dim t2 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))

        t1.Start()

        t2.Start()

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created multiple threads (t1 & t2) and trying to execute the PrintInfo method. Here, in PrintInfo method we didn’t used any SyncLock statement so the threads will execute PrintInfo method simultaneously.

 

When we execute the above program, we will get the result like as shown below.

 

i value: 1

i value: 1

i value: 2

i value: 2

i value: 3

i value: 3

i value: 4

i value: 4

If you observe the result, multiple threads started executing the PrintInfo method simultaneously.

 

This is how we can use SyncLock statement in visual basic to acquire mutual-exclusion of lock for the specified block of code to make sure that at a time only one thread can execute it based on our requirements. 

 
 
 

Topics Covered