What Are Threads (What is a Thread)?

A simple explanation would be that you have a job to do, and you get a single person to do the job.

This single person is similar to a thread in a computer.

One person can do one thing at a time, so to do the job, he proceeds through the tasks of the job, one task at a time, one operation on each task at a time.

To speed up a job, you can put more people to the same job. For instance, let's say you need to paint a house. You hire 4 people to do this.

These 4 people could be similar to 4 threads in that they work with the same resources (same house, same buckets of paint) and they can divide up the work.

A process would be akin to the job of painting that house.

This simple explanation somewhat breaks down when it comes to machines that doesn't have enough CPU cores to run all the threads simultaneously, but I'm going to ignore this here.


Something no one every took the time to explain to me was the difference between a process and a thread. Once you understand that, where threads fit makes a lot of sense.

An operating system gives a process memory to use. A process when started usually has one "thread" running within it.

The thread is what the operating system schedules to run on the CPU and it is given an address to start executing instructions from.

Some people who were much smarter than me worked out that making processes in most operating systems was much more expensive than creating a thread of execution. Additionally two threads in the same process have the ability to access the processes memory without using an operating system call and/or shared memory to do so, meaning that although you now need to synchronise the threads memory access, you could do more work in less time.

Thus threading is an important concept to understand and it's main use was to increase the performance of programs which had concurrency that could be exploited, the first major use (EDIT: This may not have been the "first" use) being running the GUI of an application on one thread, and performing processing on another, the cornerstone of modern user interface design.


An individual thread is so named because it is a single thread of execution through your code. If you have multiple threads then you have multiple threads of execution through your code simultaneously (or as simultaneously as your single/multicore system supports). Both threads have access to the same heap though use different stacks. This means data in your program may be visible by both threads and may be changed by either thread. This can of course lead to serious problems which requires protection against.

It is worth noting that a thread is different to a process. A key difference is that two threads can access the same data (heap) while two processes cannot.

For a more full description see other online descriptions

wikipedia