Feeds:
Posts
Comments

Posts Tagged ‘programming’

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 »

There are several programming techniques to avoid bugs in our programs, the problem is no one uses them!

Seriously, I don’t know of any programming technique that keeps you from adding some bugs here or there in your programs, regardless of the programming language, the programming platform or environment, or even the programming methodology or practices you are using. The only one “technique” I know of, and I use to rely on it a lot, to avoid bugs in my programs or pieces of code is careful attention, and tons and tons of tests before actually letting it go. I usually don’t trust computers, even my own code, until I see it working right for the most part or for 99% of the time. And if I understand correctly, there is only one way of producing 100% bug free code…. Not writing it.

Below is a link to a post from Wil Shipley’s blog at Blogger.com where he discusses some programming tips for young and not so young programmers about how we should code when we want to code something. Just to give you a taste of what he says:

“Think first. Think some more. Think about the whole problem. Think about a little part of the problem you’re going to start with. Think about the whole thing again, in relation to your idea on the starting point.”

If you noticed, there is not one line of  code written yet. Give it a read, it is really good and highly recommended.

http://wilshipley.com/blog/2005/02/free-programming-tips-are-worth-every.html

Since there’s no way to have programs without writing code, one good way to avoid bugs is to pay attention to whatever code we are writing, as well as verifying our code by letting others see it or review it. Although there is no warranty, this method has worked very well for me in the past. There’s no such thing as avoiding bugs by not writing any code.

Questo, que lotro…, salud!


Read Full Post »