Threads

A process is a running instance of a given program. A program runs sequentially handling one task at a time, however we may need to run certain tasks in parallel.

Think of a program that is downloading a resource from the internet and wants to update the UI at the same time to show the download process.

If the program is strictly sequential, once the program starts the download, the UI is neglected since the CPU time dedicated to the process must be focused elsewhere.

We need the UI update and the download to execute in parallel. This is achieved via operating system threads of execution.

A thread is a schedulable unit of execution within a process. It runs within a process, shares its memory address and can execute any program code loaded within it.

Each thread has an identifier called a thread ID (TID).

Both procesess in user space and the kernel utilise threads.

Hardware considerations

Each processor core can only run one thread at a time so the number of cores that a device’s CPU has deterimines how many threads can run at once. In practice, then, the CPU shuffles between multiple threads in clock segments. This is known as scheduling and is managed by the process that the thread belongs to.

Processes