Threads
Definition:
A thread is a single sequential flow of control within a program.
A thread runs within the context of a program's process and takes advantage of the resources allocated for that process and its environment.
Multiple threads:
Advantages of Multithreading
Improve applications responsiveness
Use multiprocessors efficiently.
Improve program structure.
Use fewer system resources
Improve performance.
Threads X Processes:
In traditional UNIX each process contains a single thread, so programming with multiple processes is programming with multiple threads.
Because a process is also an address space creating a process is expensive, while creating a thread within an existing process is cheap.
The time it takes to create a thread is on the order of a thousand times less than the time it takes to create a process.
Communicating between the threads of one process is simple because the threads share everything-address space, in particular.
User-level Threads
User-level threads are handled in user space:
They reduce kernel context switching penalties.
They are visible only from within the process, where they share all process resources like address space, open files, and so on.
When a thread needs to interact with other threads in the same process, it can do so without involving the operating system, reducing context switching penalties.
An application can have thousands of threads and still not consume many kernel resources.