views:

189

answers:

1

I'm new to multithreading and need to make sure the demo code below is implemented correctly. The actual application I need to multithread will be very similar, except I will have to run possibly hundreds of concurrent threads. Please code review.

Imports System.Threading

Public Class FooBar

    Private myData As New List(Of String)()
    Private rwLock As New ReaderWriterLockSlim()

    Public Function Foo() As List(Of String)

        Dim number As Integer = 3
        ' TODO: why does threads have number+1 elements after next line? '
        Dim threads As System.Threading.Thread() = New Thread(number) {}
        Dim i As Integer = 0

        While i < number
            threads(i) = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf Bar))
            threads(i).Start()
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While

        i = 0
        While i < number
            threads(i).Join()
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While

        Return Me.myData

    End Function

    Private Sub Bar()

        rwLock.EnterWriteLock()
        myData.Add("foo bar")
        rwLock.ExitWriteLock()

    End Sub

End Class
A: 

Please refer the below link

http://vb.net-informations.com/communications/vb.net_multithreaded_Socket_programming.htm

gever

gever