Posted by: Sourav | October 6, 2016

Protecting shared resources from concurrent thread access in VB,VB Teacher Sourav,Kolkata 09748184075


Option Strict On
Option Explicit On

Imports System
Imports System.Threading
Module Module1
Public total As Integer = 0
Dim _lock As Object = New Object
Sub Main()
‘addonemillion()
‘addonemillion()
‘addonemillion()
Dim stopwatch As Stopwatch = stopwatch.StartNew
Dim thread1 As Thread = New Thread(AddressOf addonemillion)
Dim thread2 As Thread = New Thread(AddressOf addonemillion)
Dim thread3 As Thread = New Thread(AddressOf addonemillion)
thread1.Start()
thread2.Start()
thread3.Start()
thread1.Join()
thread2.Join()
thread3.Join()
Console.WriteLine(“The result is {0}”, total)
stopwatch.Stop()
Console.WriteLine(“elapsed ticks are {0}”, stopwatch.ElapsedTicks)
Console.ReadLine()

End Sub

Public Sub addonemillion()

Dim i As Integer
For i = 1 To 1000000
‘total = total + 1
Interlocked.Increment(total)
‘SyncLock _lock
‘ total += 1
‘End SyncLock
Next
End Sub
End Module

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: