RSS

Single Vs. Multi threading

16 Nov

Imports System.Threading

A multi-threaded app requires significantly more brainpower, testing and patience to complete than its sequential counterpart. Its my advice to make sure you get a significant performance boost and time/results ratio before tackling a huge re-write. If your teammates or the future maintainers of the app don’t have the skill set, it may cost you in the long run. Before you read I am assuming you have written single threaded applications that use functions and parameters.

To make the switch from single threaded to multi threaded code, the trick is to create classes that can be instantiated by any thread. This way you can get variables or data in and out of your routines easily. To pass a parameter into a thread you would use the overload function of thread.start().

'Single threaded version
        For Each item In todolist
            myFunction(item)
        Next
'multi threaded version
        For Each item In todolist
            Dim t As New Thread(AddressOf oClass.myFunction)
            t.Start(item)
        Next

Kicking it off isn’t that difficult. In that example “item” is the variable that is passed into “myFunction”. To get a variable out of the single threaded example we could just “Return” it. The multi threaded example we’ll have to catch a thrown event. Here is the myFunction wrapper “oClass” in which you can see how to define the “evtReturn”

    Public Class oClass
        Public Event evtReturn(ByVal RetVal As String)
        Public Sub myFunction(ByVal myInput As String)
            'Do Stuff
            Dim myOutput as string = myInput
            RaiseEvent evtReturn(myOutput)
        End Sub
    End Class

The definition of the “FinishedWithAThread” routine has an important part “Handles oClass.evtReturn” These events may come back simultaneously so we have to use caution. This is a good time to use “synclock” to prevent an IO exception.

Sub FinishedWithAThread(ByVal myFunctionReturn as string) Handles oClass.evtReturn
        SyncLock GetType(oClass)
            File.AppendAllText("output.txt", myFunctionReturn & System.Environment.NewLine)
        End SyncLock
End Sub

With this newly acquired skill it will be fairly easy to grind any CPU into the ground. If you are writing an app for a server with multiple users, you may not even need to multi thread because the users are doing that for you. Then again, if that same server has 256 cores…. give it all you’ve got.

Advertisements
 
1 Comment

Posted by on November 16, 2010 in .NET

 

One response to “Single Vs. Multi threading

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

 
%d bloggers like this: