libpthread API Specification |
Thread is a schedulable entity that runs within a process. In other words, a thread is a unit of execution that has its own stack and executes a given piece of code. Unlike a real process, the thread normally shares its memory with other threads (whereas processes usually have a different memory area for each one of them). A thread group is a set of threads all executing inside the same process. They all share the same memory, and thus can access the same global variables, the same heap memory, the same set of file descriptors, and so on. All the threads in a thread group execute in parallel (that is, using time slices, or if the system has several processors, then really in parallel). This implementation is currently user-space only.
The advantage of using a thread group instead of a normal serial program is that several operations may be carried out in parallel, and thus events can be handled immediately as they arrive (for example, if one thread is handling a user interface, and another thread is handling database queries, we can execute a heavy query requested by the user, and still respond to user input while the query is executed).
The advantage of using a thread group over using a process group is that context switching between threads is much faster than context switching between processes. In addition communication between two threads is usually faster and easier to implement than communication between two processes.
On the other hand, because threads in a group all use the same memory space, if one of them corrupts the contents of its memory, other threads might suffer as well. With processes, the operating system normally protects processes from one another, and thus if one corrupts its own memory space, other processes will not suffer.
Each process will have one main thread, and it may also have several more threads which share the address space of the process. Threads do not have a parent-child relationship with each other.
©Nokia 2007 |