Threads and Threading

Threads are a method of dividing a process or program so that different parts of it may execute (or may seem to execute) in parallel, while still sharing the same address space.

Threads are a useful technique for making an otherwise complicated procedure where disparate functions need to be performed without much (but usually with some) coordination. For example, many graphical user interface systems are threaded so that the process can handle the User Interface and the Primary Function Code in logically distinct units while still being able to have the two interface with each other through a simple interface (memory).

A parallel concept (but perhaps not obviously so) to threads is Events.

Types of Threading (Threading Models)

There are two types of threading: Cooperative Threading and Preemptive Threading

If you want Cooperative, Protothreads is by far the most portable (GNU Pth, for example, won't run under win32).

If you want Preemptive, currently the best solution for portable applications seems to be POSIX Threads (pthreads), and a wrapper (library, or a simple header if possible) for places that don't support POSIX Threads (notably Win32 without "Services for Unix" installed on the client).

More About Events

Doctor John Ousterhout (principal designer of the Tcl language) wrote a paper about Events called "Why Threads are a Bad Idea (for most purposes)" [1] [2]

Give Me an Example

The same program written using various threading techniques (the same style is used for clarity, even if it causes inefficiency (i.e., by not using pthread_join)):

Win32 Pthreads information

Other information