Friday, 7 September 2012

Introduction to Multithreading & Concurrency Part 1

Concurrency and Multithreading in Programming are two areas in which I have been interested in for a fair while now, but for some reason I have neglected to look into them properly. I therefore decided to fix this once and for all and have decided to invest some of my spare time and investigate these topics.

For those of you who do not know much about Multithreaded programming or concurrency I'll start by giving a brief explanation of these concepts. First up is a non-computer/code related example.

At the simplest level concurrency is allowing two or more separate activities to happen at the same time. A really simple way to think of this is to think of activities that you yourself carry out each day - walking while talking, performing different activities with each hand and probably the most important example, the ability to go about your lives independently of everyone else. For instance you can play a video game while someone else does a completely different activity.

In computer terms concurrency and multithreading essentially is the ability to run multiple pieces of code in parallel with each other rather than running the code sequentially. These pieces of code can therefore be completely independent of each other but they are allowed to depend closer on each other if needs be.

Just like everything else code related there are multiple ways to go about it but there are two main approaches when it comes to concurrency. To easier describe these approaches think of this scenario:

Two people work in the same building. If each person has his own office then it allows them to go about their work in peace without being disturbed by the other person. It also allows as them to have all their own materials and resources. Communication between the two people however is not straightforward; they either have to phone each other, email or visit the other person's office. They do not have the ability to just turn round and speak to the other person. There is also the downside of having to manage the overheads of two offices, two sets of resources etc.

On the other hand if there is two people that work in the same office then communication between the pair is extremely easy and there is no need to have the overheads for having more than one office or more than one set of resources. The downside to this is lack of peace for each worker and not being able to have the resources available to you at all times i.e. the other person may be using them.

Both of these examples are essentially the approaches that can be taken when it comes to concurrency. If each person represents a thread and each process is represented by an office then the first example is the same as having multiple single-threaded processes whereas the second approach is the same as having multiple threads in a single process. These approaches can be combined of course and you can have multiple processes, some of which are mulithreaded and some of which are not, but the basic principles still remain the same.

It is the latter of these approaches that I am going to focus on - Concurrency with Multiple Threads.

A thread is essentially a lightweight process: each thread runs independently of the others, and each thread can run its own set of instructions. Every thread however shares the same address space and most of the data can be accessed from all threads. Global variables remain global and references and pointers can be passed around among threads.

Because the overhead on memory is generally lower when launching multiple threads within a single process as opposed to launching multiple single-threaded processes means that this is the usual approach when using concurrency especially within C++. However this method is not without its own problems such as the management of shared resources. I will go into much more detail regarding this in further posts. 

Concurrency is not always the best solution however and each project will have to be carefully developed to ensure that multithreading and concurrency are worthwhile. Again this is something that I will go into more detail about it the future.

In the next post I will go into some more detail regarding Concurrency and Multithreading and more importantly I will be posting some code that will better explain methods of concurrency and multithreading in C++.

Until next time,
Thanks for reading,
                                James
 

No comments:

Post a Comment