Posts Tagged ‘advanced’

Whenever we create our first program we have no idea about what multithreading is or what a thread is for that matter, and no one cares explaining it at the time, or so it seems. However, whenever we create or first program we are already programming using threads, yes one thread at least.

A thread, in programming, is not a physical thing or something inside the computer. It is jut a term to refer to a programming concept or technique.

When we run our first best program, the operating system loads it (accommodates our program in memory), then the computer starts executing its instructions one after another, one at a time, and if there are no more instructions to execute the computer finishes execution, and our program terminates. Done deal, the program ended. So what?

Let me see if this explains it…. That was a thread of execution. Some people also call it a context of execution.

Typically we will add a loop in our program so that the computer keeps it loaded and keeps “running” those instructions inside the loop. Whenever the user decides to leave the program we would break out of the loop and our program would then terminate and exit. That’s it. Only one executing thread but a continuously running thread.

How did I know a program is using one thread? I didn’t. It turns out that when the operating system loads our program it will usually create a process for it, and then it creates a thread or context to execute whatever we wrote in our program; this thread is usually called the main thread or the main context of execution. That easy…, thread programming! Single-thread programming if you wish.

So what?? Well, if we want to do multi-threading or multi-thread programming, we have to create any additional threads programmatically (manually); the operating system will create only one for us, the main one but, any additional threads have to be created by the programmer. Don’t worry, you don’t have to pay a penny to create additional threads in your program. Additional threads are free… Cool!

Ok I see it….. If I create an additional thread in my program then I am doing multi-threading programming. Yep.

And why would I want to create an additional thread in my program?

There are many reasons for multi-threading but let me mention just one. Often times we have to process long running or time consuming tasks in our program (for instance, downloading big files) but, we don’t want to have users waiting for that long running task to finish and being unable using our program. Thus, we want to have the computer execute time consuming tasks in a separate thread of execution; that way users can keep working with the main program at the same time a file is downloading.

If we want to create an additional thread in our program, we have to provide the starting point of execution for that thread, which is usually the name of a function in our program that we want that thread to execute.

Then again…..

When we create our first best thread in our formerly single-threaded program, the operating system will accommodate the additional thread in memory, and the computer will start executing the instructions contained in the function we specified when we give it the go ahead (usually calling Start()), one after another, one at a time, and if there are no more instructions to execute in our function the context of execution terminates. Done deal, the thread is finished and very likely disposed of by the operating system. Thank you for participating!

Yep; if we do not write a loop (which we would usually do for most long processing tasks) in our ‘to be executed by an additional thread’ function, the computer will run whatever there’s inside that function until the end and the thread will end. That’s it; one less thread in our program. This means two ways of ending your threads; one is by not having a loop in the function executed by the thread; second one is by having a loop in the function and breaking out of the loop when a certain condition is satisfied, which will end the thread automatically. As far as I know, programming languages that support multi-threading have no function to stop a running thread. Stopping a running thread will depend on whatever we write inside the function executed by the thread.

Starting the execution of an additional thread is not the same as calling a function in our program, nope; starting the execution of an additional thread in our program is starting execution of an additional context “in parallel” (or so it seems) with our main context. The computer will continue with the execution of our main thread right after we start our additional thread; at this point the computer will be running the two threads in our program, two execution contexts, the main context and our new thread context. Wonderful! And the operating system will take care of switching execution contexts back and forth from the main thread to the new thread; we as programmers don’t have to worry about switching between threads after we have started additional threads in our program. Fantastic!

Note that if there’s no loop in our main thread and we created an additional thread having a loop, our main program will end executing and our extra thread with it, most likely; the garbage collector will probably take care of that additional thread once its own loop ends, but who knows what will happen; I don’t know. It will depend on the programming system you are using.

The “complexity” of multi-threading starts with the “execution in parallel” part. For one, you the programmer is responsible of avoiding concurrency conflicts when your different threads try to change or access the value of a ‘common to all threads variable’ (commonly known as global variable); remember our threads are “executing in parallel” and we won’t know when and in what order the computer will switch execution context (switching execution from one thread to another thread). Programming languages that support multi-threading have functions that we can use to synchronize our threads, that is, having our threads access global variables in a synchronized manner. However, your program and your threads should run fine if you manage to keep your threads (contexts of execution) from touching other threads’ belongings, or if you keep your threads in sync when touching ‘common to all threads’ variables, also known as static variables.

I think that’s very much all there is to understand multi-threading (remember it is “executing in parallel”). If  you are creating your first program and you understood this explanation you should be able to create multi-threading programs from now on. Good luck.

Questo, que lotro…, salud!

Read Full Post »