An application implemented as a single process follows a single path of execution through the program. Most (All) applications you’ve written at this point are single process applications.
When a process is blocked by a resource, then the whole process is blocked.
However, many applications have other parts of the application that could continue to run if needed. For example, let’s say you want to save a document, well, if the HDD/SDD is blocked, your whole application would freeze until that resource became available.
Instead, you can spin off a thread, and have that thread wait for the resource, while your main application continues running and your user can keep using it.
Another example we looked at earlier for child processes, which are similar in idea to threads. However, threads can be created easier and faster than a child process.
Previously we mentioned the idea of spell check. A thread constantly checks spelling while you are typing. That’s because it is a separate thread running in the background while you are typing.
Servers often work with lots of threads. As a new request from a different user comes in, a thread is generated to check the status/perform an operation/etc. This is more efficient, than having users queue up for a single thread process. Once the job is done, the newly created thread often times out and terminates.
Some servers use pools of threads to reduce the number of creation and termination of processes, as each time a process is created or terminated, it takes time to create/destroy that PCB. They only create new threads as needed when the pool is full.
Threads may execute the same, or different, parts of the main program from the main process. However, the program code and data don’t have to be reloaded, which makes if faster than loading the same application again.
Threads can help take advantage of multiple CPUs.
A thread control block (TCB) is a data structure that holds a separate copy of the dynamically changing information necessary for a thread to execute independently. The replication of only the bare minimum of information in each TCB, while sharing the same code, global data, resources, and open files, is what makes threads much more efficient to manage than processes.
Introduction to Threads was originally found on Access 2 Learn