8.1. Overview of threads

A thread is a single, sequential flow of control within a process. Within each thread, there is a single point of execution. On most UNIX operating systems, including AIX, a thread possesses the following characteristics:

  • It has its own independent flow of control within a process.

  • It shares resources with other threads within a process.

  • It can voluntary terminate before the process termination, or all the threads within a process terminate when the process terminates.

Most traditional programs execute as a process with a single thread. From a programmer’s point of view, a thread can be considered as a procedure that can concurrently and independently run from the main processing flow.

8.1.1. Relationship between a process and a user thread

A process is a running program instance, which contains the following resources:

  • Address space, including program text, shared library, global data, and so on.

  • File descriptors

  • Signal handlers

  • Environment (variable) and working directory

  • Set of identifiers (PID, GID, and so on)

  • Inter-process communication (IPC) facilities, such as message queues, pipes, semaphores, and shared memory segments.

Multiple user threads can exist within a process and use these process resources, yet are able to be scheduled by the operating system and run as independent entities within a process. A user thread can posses an independent flow of control and can be scheduled because it maintains its own resources:

Figure 8-1 illustrates the relationship between a process and two user threads.

Figure 8-1. Two user threads in a process


A process can have multiple user threads, all of which share the resources within a process and all of which execute within the same address space. At any given point in time, there are multiple points of execution within a multi-threaded process.

Because user threads within the same process share resources, changes made by one user thread to a process resource will be transparently seen by all other user threads. Also, because multiple user threads can read and write the same memory locations within a process address space, the synchronization of data must be explicitly taken among these threads.

The cost to create or manage user threads is relatively cheap compared with processes in terms of CPUcycles. Also, the creation of a user thread requires a very small amount of memory compared with a process creation. With careful design and coding, the use of user threads gives programmers the ability to write multi-threaded applications that run on both uni- and multi-processor systems, taking advantage of the additional processors on SMP systems. Additionally, multi-threaded applications can increase performance even in a uniprocessor environment when the application performs operations that are likely to block or cause delays, such as file or socket I/O.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset