A thread is a single sequential flow of control within a process. Threads are the basis for multithreaded programs, in which multiple independent but related threads cooperate to accomplish a larger task. Threads are lightweight processes that reduce scheduling overhead in several ways. The most significant way is that threads reduce the need to swap in new memory segments on a context switch, because threads share the same memory space. Multithreaded programs generally use reentrant code (code that multiple threads can use simultaneously) and are most valuable when run on multiple-CPU machines. Under Linux, multithreaded servers, such as NFS, may be easier to write and maintain than multiple server processes. When applied judiciously, multithreading can also serve as a lower-overhead replacement for the traditional forkexec idiom for spawning processes. Fedora Core 5 and Red Hat Enterprise Linux 5 no longer support LinuxThreads (linas.org/linux/threads-faq.html). Instead they support the Native POSIX Thread Library (NPTL). NPTL implements POSIX threads and improves performance and scalability over LinuxThreads. See tldp.org/FAQ/Threads-FAQ and page 1012 for more information about threads. Tip: Multiple threads are not always better If you write a multithreaded program with no clear goal or division of effort for a single-CPU system (for example, a parallel-server process), the resulting program will likely run more slowly than a nonthreaded program would on the same system. |