Multithreading is a process which helps us to do multitasking. Multithreading is very useful for saving time and improving performance, but it cannot be applied everywhere.
Multithreading allows us to multiple tasks which are unrelated processes. These processes do not share their resources and communicate through IPC.
For knowing more in multithreading we have to know what is thread.
A thread is a sequence of instructions in a program which can be executed independently of other code. We can also say that thread is subset of a process and a process is made up of many threads.
Threads sometimes called light-weight processes, and they do not require much memory overhead; they are cheaper than processes.
A thread has a starting point, an execution sequence, and a result.
A thread contains all this information in a Thread Control Block:-
- Thread Identifier- Unique id is assigned to every thread.
- Stack pointer- Points to thread’s stack in the process. Stack contains the local variables under thread’s scope.
- Program counter- A register which stores the address of the instruction currently being executed by thread.
- Thread state- Can be running, ready, waiting, start or done.
- Thread’s register set- Registers assigned to thread for computations.
- Parent process Pointer- A pointer to the Process control block (PCB) of the process that the thread lives on.
After knowing thread we can define multithreading as an ability to execute multiple threads simultaneously.
This is all about threads in python.