At any given time our computer runs many different programs at the same time. If we open our process monitoring tool, we’ll see 10s or 100s of progr

Threads vs lightweight threads

submited by
Style Pass
2024-09-28 11:00:05

At any given time our computer runs many different programs at the same time. If we open our process monitoring tool, we’ll see 10s or 100s of programs running. In order for a program’s code to run, it needs to be executed on the CPU. However, code does not execute directly on the CPU, but needs a thread to do that.

A thread is the entity that carries the code that needs to be executed. To visualize, we can say that a program’s code runs on a thread, and a thread runs on a CPU:

Threads exist in any modern operating system like Linux and Windows. It’s an abstraction layer between the CPU and the program you want to run. Before a program’s code is executed, it is assigned a thread. The thread contains information about exactly what part of the program’s code is running. For instance, it contains the memory address of the instruction that is currently executing, registers and call stack.

A program can start many threads, executing different parts of the same program. The whole magic of threads is that it can be stopped and later resumed. When a thread is stopped, all state information such as registers and call stack of that thread is stored in memory. When it resumes, this state information is loaded from memory so it can continue executing.

Leave a Comment