What is the definition of thread?

A computer runs many applications at once, each instance of an application is known as a process. Each process is made of 1 or more threads, each thread is a sequence of code, this code is often responsible for one aspect of the program, or one task a program has been given. For instance a program doing a complex long calculation may split into two threads, one to keep a user interface responsive, and one (or more) to progress through the lengthy calculation.

The catch is that when dealing with one or more threads, whilst it is guaranteed each individual thread will progress through its code in sequence, it is not known where each thread will be relative to each other. That is, one thread may progress more quickly than other threads, this means great care must be taken when two threads access one resource, this is usually done through a mutex.


In computing terms, a thread is a separate line of execution inside a process which shares the instruction portion of the process, but which has unique data associated with that thread.

To use an analogy: let's assume we have to make 10 cakes. A normal process would start with its instructions (the cake recipe), and the data (the ingredients for the 10 cakes), then follow the recipe iterative (repetitively) 10 times in sequence, producing 10 cakes. A threaded model for this would say: "but, making cake 4 does not require the exact same egg being used to make cake 2 (that is, there is an egg (data) for each cake)." So, using threads, each thread would use the same instructions (recipe), but have its own unique data (set of ingredients).

The problem with threads (as brought up above) is when they must share some resource. In our example, let's assume we only have 1 oven. So, each of the threads would go about preparing it's cake, then go over to use the oven. The first thread to get there would make a lock (mutex) on the oven to bake its cake. The remaining threads would sit around and wait for the lock (mutex) to be cleared, then one of them would grab it, and bake its cake.