Wednesday, November 3, 2010

Multi-threading With Visual Basic

Ok, today we are going to talk about multi-threading.  I would give this lesson a difficulty of MODERATE not because it is difficult to code it, but because multi-threading can be a bit confusing conceptually.

First off, what is multi-threading and why is it useful?  Well, when your program is running normally, aka you are not using multi-threading, it can only execute one instruction at a time.  Only one line of code is running at any given time.  If you use multi-threading, you can be running as many lines of code at once as you want.  Now don't go and try to make a program where each line of code has its own thread and think you have discovered ultimate efficiency, because there are a few catches to multi-threading.  Any segments of code that run at the same time cannot be dependent on each other.  For example, if you have two functions, and the output of the first function is used as input for the second, you cannot run them at the same time.  If the first one isn't finished, the second one can't start.  Another catch to multi-threading is that if you are using a single core computer, the benefits of multi-threading will not be as great.  This is because when there are multiple threads on a single core computer, they cannot truly run at the same time, so instead, the take turns running a little bit.  This occurs so quickly that they appear to be running at the same time, but in reality they are not.  If you have a computer with more than one processor core, then your computer can actually run more than one instruction at the same time. .

On to the programming...

To create a new thread, you declare a variable of type thread.

Dim newThread as Thread


Then you must initialize it as a new thread.  The new function for threads accepts an argument of type ThreadStart.  The best way to handle this is to create a new ThreadStart.  The argument for this new sub is 'AddressOf' and the name of the subroutine that you want the new thread to run.


newThread = new Thread(new ThreadStart(AddressOf doStuff))


That code assumes that you have defined a subroutine named 'doStuff'.  For the subroutine that you want the new thread to run, you can put just about anything in it.  In one program I created, I wanted to use every core the computer had to find prime numbers.  I did this by creating a loop that created a new thread for each processor and added them to an arraylist of threads.  Then each one was told to run the same subroutine that found prime numbers.  I made this subroutine a loop so that the threads would run continuously until I stopped them.  That is one way you can use multi-threading, but there are many more.  Say for example you are writing a game and you want the character's stats to be updated every half of a second regardless of what the program is doing. You could create a timer that would run the update subroutine on a separate thread every half a second.  This would allow the stats to update even if the rest of the program was busy.  For that example it is very important that you do not let any of the other threads modify the controls that your update thread is using, if that happens the entire universe may unravel at it's seams.  Not really but it would be bad so just don't do it.


That is about all I have to say on multi-threading, I hope you enjoyed reading this and hopefully you learned something.  Multi-threading is a powerful tool, especially in today's world of many core computers, multi-threading is the way of the future.


On a side note, I just realized that I am writing about programming and I didn't even provide a link to free software that you can use to program in. Tsk, tsk, on me...


So here it is: Visual Basic 2010 Express
To download visual basic 2010 express, just go to the visual basic header and expand it, then select the language that you prefer.  This is free software that is provided by Microsoft, so enjoy it.

Note: If this link does not work because you are reading this way after it was written, just google 'Visual Studio Express Download'
Wait, I will even do that for you: Click here to Google it!


Thanks for reading, if you have any good ideas for more topics, feel free to email them to me.