Threads
A thread is an executable unit. A task is made up of one or more threads. Each thread has its own execution stack and is capable of independent input/output. All threads share the virtual memory address space and communication rights of their task. When a thread is started, it is detached from its initiating thread. The new thread runs independently. That is, the initiating thread does not know the new thread's state.
Threads are especially useful when you need to perform a lengthy task, but do not want it to block the execution of the rest of the application. In particular, you want to avoid freezing the user interface, preventing the user from performing additional actions. Threads can also be used to divide a large job into several smaller jobs. Then, if the computer has multiple processors, the jobs can be distributed amongst them for greater efficiency.
A task has only one thread when it first starts. Additional threads can be created using either the POSIX thread APIs (pthreads) or the Cocoa NSThread class.
Consider carefully whether you need to use threads. Due to the overhead associated with proper synchronization (locking, messaging, and so on), raw performance on a single-processor system is somewhat slower for multithreaded applications. Perceived performance might actually be improved due to deferred execution, using timers. One major reward from multithreading your application, though, is improved performance on a multiprocessor system.
© 2002 Apple Computer, Inc. (Last Published October 31, 2002)