Friday, May 13, 2011

An Introduction to Threading and Multi-core Processors

An Introduction to Threading and Multi-core Processors


With the increasing popularity of multi-core processors, most people imagine that all their programs will run much faster. While this is true, it is not always the case. Sure, the operating system boots up faster, but what about Firefox, or Blender? Some people have noticed they just stay the same speed. So why do these programs run so slowly on such a “fast” processor?

The answer to this question lies in threads. So what is a thread? One can think of a thread as a lightweight process, where it can execute in a process and access the process’s resources, along with sharing information with other of its kin. Seems a bit bizarre? Do not fear, threads are quite simple to understand when you put them in an analogy. Imagine a room. In this room, there is only a filing cabinet will all the documents employees of Department A need to process. Joe, our hard-working employee works in this office, which all people of Department A share. Now, Joe’s office has a small problem - it is missing an essential piece of furniture: a desk. Without it, Joe has nowhere to place any of his work. Not to worry, the company did ensure that Joe the employee can work. So they ordered a desk. Unfortunately, they were a bit low on budget and could only afford one desk, so all the employees must share this one table. Problem is, the desk is so small that only one person can work on it. This means while Joe is filling out paperwork, Alice, an employee from a different department, must wait until Joe is finished. This may seem like it is very unproductive (and it is), but it gets worse. By a company policy, employees can only work on 10 documents during one session at the desk so other employees can use the desk too. This makes their work time slower as more people line up to use the desk if they have more than 10 documents, as they need to make trips back to the . Joe often needs to work on hundreds of documents. To increase the efficiency of his department The obvious solution is to hire another employee, and put him in the same department as Joe. So the company hires Steve and places him in Department A. Joe can then give half his work to Steve and he can wait behind Joe and as soon as Joe is finished working on 10 documents, Steve can jump to the table while Joe returns the end of the line. By employing another worker, the Department is performing faster. This technique is called multi-threading in programming.

In this analogy, Joe and Steve are threads (employees) of the same process (Department A). They can both access the documents (instructions) in their room and also work at the same pace. Now Alice who works in Department B is a thread in a different process and she can only get documents from her department. This is how processes work. Threads of one process can only access instructions and data from the process in which they were created.

Now back at the office, the Board of Directors thought it would be a good idea to increase productivity. The next day, all the employees walked into the workplace for a surprise: the company is now the proud owner of two desks. Now any of the employees can work at 2 times the efficiency. Now, to the board of directors, they had expected all the work to be done in half the time. Why was it that Alice’s department, Department B still worked at the same speed as it did before the second desk. The answer is that there is still only one employee in Department B and regardless of how many free desks, Alice can only work so fast. So single threaded applications such as Firefox are like Alice, where there is only one employee who needs to share the desk without sharing the work. This fact makes them unable to use the additional core as efficient as other programs such as Apophysis (a Fractal Renderer) that have multi-threading capabilities.

In this scenario, adding another desk is equivalent to adding an additional core to the processor. This is where you get dual core processors from Intel and AMD. This covers the basic theory of threads. This is also where I am going to finish of this article. Of course, being a introduction, it does not cover some of the more advanced topics such as:

Hyper-Threading - Where the workers literally share the desk by drawing a line down the middle and have two people work on it at the same time
Process Isolation - If there is a murder in Department A, Alice from Department B does not need to know about it.
Deadlocks - Steve is waiting for Joe to give him some data so he can finish his work but Joe is waiting on Steve to give himself data so he can give Steve the data he needs.



So if you want to know more about them, Wikipedia is a great resource. Cheers!

6 comments: