Chapter 1 – Introduction to multithreading by muskad202

Your computer most probably has only one processor.

One processor can carry out only one task at a time. But most probably, you run multiple applications simultaneously. How do they all work together even though you have only one processor? The answer is – multitasking. The operating system on your machine allocates the use of the processor to the different applications. It might tell application A to use the processor, then, after a few milliseconds, it will step in and pause application A, and then tell application B to use the processor, and then again, after a few milliseconds, pause application B, and tell application C … and so on.

Every application runs in at least one thread. What is a thread? It’s a little difficult to explain – but think of it as a sort of “bubble” containing code which can execute sequentially. When a thread is active, all the code within it executes sequentially, one line after another. If your application has only one thread, then, when your application is given permission by the operating system to use the processor, the code in your thread begins executing line by line.

Lets move on to an example that will make it, (and why multithreading is required) a bit more clearer.

Create a new Windows Application. To the form that is auto-created, add a button. In the Click event handler of the button, enter the following code:

        private void button1_Click(object sender, EventArgs e)
        {
            while (true)
            {
                int x = 5;
                int y = 6;
                int z = x + y; 
           }
        }

 

As you can see, we are just executing an infinite loop, where we calculate the sum of 5 and 6.Compile the application, and run it. Click the button when the form comes up. Now, try moving the form around. You wont be able to. Why? That’s because your application has only one thread. And in that thread, as I mentioned above, code gets executed sequentially. When you clicked the button in your form, the code in the event handler above begins to execute. And since its an infinite loop, that same code runs again and again. The code that actually handles the moving around of the form never gets a chance to execute. You might ask – why do all my other applications yet run, even though this form has “hung”? Well, that’s because the operating system gives all applications a chance to run. After allowing your application to run for some time (the time which it wastes in an infinite loop), the operating system takes away control of the processor and gives it to another application. When control finally returns to your application, it goes back to your thread, which resumes executing its infinite loop.

So how would multithreading help here? Suppose in the Click event handler, you create another thread, and in that thread run your infinite loop. So, your application now has two threads running. When your application gets control of the processor, each of your threads gets a portion of this time to use your processor. The thread you created would use its time to execute its infinite loop, while the original thread would be ready to handle user interaction – such as you moving the form around the screen.

I will not show you how to modify the application above so that the infinite loop executes in another thread – you can try that out yourself after going through the remaining four chapters.