Dividing C++ Programs into Multiple Threads
"As our computer systems become more complicated, this kind of abstraction gives us hope of being able to continue to manage them."
Andrew Koening and Barbara Moo, Ruminations on C++
In this Chapter
Threads: A Definition
The Anatomy of a Thread
Introduction to the Pthread Library
The Anatomy of a Simple Threaded Program
Thread Safety and Libraries
Dividing Your Program into Multiple Threads
The work of a sequential program can be divided between routines within a program. Each routine is assigned a specific task, and the tasks are executed one after another. The second task cannot start until the first task finishes, the third task cannot start until the second task finishes, and so on. This scheme works fine until performance and complexity boundaries are encountered. In some cases, the only solution to a performance problem is to allow the program to do more than one task simultaneously. In other situations the work that routines within a program have to do is so involved that it makes sense to think of the routines as mini-programs within the main program. Each mini-program executes concurrently within the main program. Chapter 3 presented methods for breaking a single process up into multiple processes, where each process executes a separate program. This method allows an application to do more than one thing at a time. However, each process has its own address space and resources. Because each program is in a separate address space, communication between routines becomes an issue. Interprocess communication techniques such as pipes, fifos, and environment variables are needed to communicate between the separately executing parts. Sometimes it is desirable to have a single program do more than one task at a time without dividing the program into multiple programs. Threads can be used in these circumstances. Threads allow a single program to have concurrently executing parts, where each part has access to the same variables, constants, and address space. Threads can be thought of as mini-programs within a program. When a program is divided into separate processes, as we did in Chapter 3, there is a certain amount of overhead associated with executing each of the separate programs. Threads require less overhead to execute. Threads can be thought of as lightweight processes, offering many of the advantages of processes without the communication requirements that separate processes require. Threads provide a means to divide the main flow of control into multiple, concurrently executing flows of control.
4.1 Threads: A Definition
A thread is a stream of executable code within a UNIX or Linux process that has the ability to be scheduled. A thread is a lighter burden on the operating system to create, maintain, and manage because very little information is associated with a thread. This lighter burden suggests that a thread has less overhead compared to a process. All processes have a main or primary thread. The main thread is a process's flow of control or thread of execution. A process can have multiple threads and therefore have as many flows of control as there are threads. Each thread will execute independently and concurrently with its own sequence of instructions. A process with multiple threads is called multithreaded. Figure 4-1 shows the multiple flows of control of a process with multiple threads.
Figure 4-1. The flows of control of a multithreaded process.
4.1.1 Thread Context Requirements
All threads within the same process exist in the same address space. All of the resources belonging to the process are shared among the threads. Threads do not own any resources. Any resources owned by the process are sharable among all of the threads of that process. Threads share file descriptors and file pointers but each thread has its own program pointer, register set, state, and stack. The threads' stacks are all within the stack segment of its process. The data segment of the process is shared with its thread. A thread can read and write to the memory locations of its process and the main thread has access to the data. When the main thread writes to memory, any of the child threads can have access to the data. Threads can create other threads within the same process. All the threads in a single process are called peers. Threads can also suspend, resume, and terminate other threads within its process.
Threads are executing entities that compete independently for processor usage with threads of the same or different processes. In a multiprocessor system, threads within the same process can execute simultaneously on different processors. The threads can only execute on processors assigned to that particular process. If processors 1, 2, and 3 are assigned to process A, and process A has three threads, then a thread will be assigned to each processor. In a single processor environment, threads compete for processor usage. Concurrency is achieved through context switching. Context switches take place when the operating system is multitasking between tasks on a single processor. Multitasking allows more than one task to execute at the same time on a single processor. Each task executes for a designated time interval. When the time interval has expired or some event occurs, the task is removed from the processor and another task is assigned to it. When threads are executing concurrently within a single process, then the process is multithreaded. Each thread executes a subtask allowing these subtasks of the process to execute independently without regard to the process's main flow of control. With multithreading, the threads can compete for a single processor or be assigned to different processors. In any case, a context switch occurring between threads of the same process requires fewer resources than a context switch occurring between threads of different processes. A process uses many system resources to keep track of its information and a process context switch takes time to manage that information. Most of the information contained in the process context describes the address space of the process and resources owned by the process. When a switch occurs between threads in different address spaces, a process context switch must take place. Since threads within the same process do not have their own address space or resources, less information tracking is needed. The context of a thread only consists of an id, a stack, a register set, and a priority. The register set contains the program or instruction pointer and the stack pointer. The text of a thread is contained in the text segment of its process. A thread context switch will take less time and use fewer system resources.
4.1.2 Threads and Processes: A Comparison
There are many aspects of a thread that are similar to a process. Threads and processes have an id, a set of registers, a state, a priority, and adhere to a scheduling policy. Like a process, threads have attributes that describe it to the operating system. This information is contained in a thread information block similar to a process information block. Threads and child processes share the resources of its parent process. The resources opened by the process (main thread) are immediately accessible to the threads and child processes of the parent process. No additional initialization or preparation is needed. Threads and child processes are independent entities from its parent or creator and compete for processor usage. The creator of the process or thread exercises some control over the child process or thread. The creator can cancel, suspend, resume, or change the priority of the child process or thread. A thread or process can alter its attributes and create new resources but cannot access the resources belonging to other processes. However, threads and processes differ in several ways.
126.96.36.199 Differences between Threads and Processes
The major difference between threads and processes is each process has its own address space and threads don't. If a process creates multiple threads, all the threads will be contained in its address space. This is why they share resources so easily and interthread communication is so simple. Child processes have their own address space and a copy of the data segment. Therefore, when a child changes its variables or data, it does not affect the data of its parent process. A shared memory area has to be created in order for parent and child processes to share data. Interprocess communication mechanisms, such as pipes and fifos, are used to communicate or pass data between them. Threads of the same process can pass data and communicate by reading and writing directly to any data that is accessible to the parent process.
188.8.131.52 Threads Controlling Other Threads
Whereas processes can only exercise control over other processes in which it has a parentchild relationship, threads within a process are considered peers and are on an equal level regardless of who created whom. Any thread that has access to the thread id of another thread in the same process can cancel, suspend, resume, or change the priority of that thread. In fact, any thread within a process can kill the process by canceling the main or primary thread. Canceling the main thread would result in terminating all the threads of the processkilling the process. Any changes to the main thread may affect all the threads of the process. When changing the priority of the process, all the threads within the process that inherited that priority and have not changed its priorities would also be altered. Table 4-1 summarizes the similarities and differences between threads and processes.
4.1.3 Advantages of Threads
There are several advantages of using multiple threads to manage the subtasks of an application as compared to using multiple processes. These advantages include:
Less system resources needed for context switching
Increased throughput of an application
No special mechanism required for communication between tasks
Simplify program structure
Table 4-1. Similarities and Differences between Threads and Processes
Similarities Between Threads and Processes
Differences Between Threads and Processes
Both have an id, set of registers, state, priority, and scheduling policy. Both have attributes that describe the entity to the OS. Both have an information block. Both share resources with the parent process. Both function as independent entities from the parent process. The creator can exercise some control over the thread or process. Both can change their attributes. Both can create new resources. Neither can access the resources of another process.
Both have an id, set of registers, state, priority, and scheduling policy.
Both have attributes that describe the entity to the OS.
Both have an information block.
Both share resources with the parent process.
Both function as independent entities from the parent process.
The creator can exercise some control over the thread or process.
Both can change their attributes.
Both can create new resources.
Neither can access the resources of another process.
Threads share the address space of the process that created it; processes have their own address. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent
Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate
with sibling processes.
Threads have almost no overhead; processes have considerable overhead. New threads are easily created; new processes require duplication of the parent process. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child
Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process;
changes to the parent process does not affect child processes.
Threads share the address space of the process that created it; processes have their own address.
Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
Threads have almost no overhead; processes have considerable overhead.
New threads are easily created; new processes require duplication of the parent process.
Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.
184.108.40.206 Context Switches during Low Processor Availability
When creating a process, the main thread may be the only thread needed to carry out the function of the process. If the process has many concurrent subtasks, multiple threads can provide asynchronous execution of the subtasks with less overhead for context switching. When processor availability is low or there is only a single processor, concurrently executing processes involve heavy overhead because of the context switching required. Under the same condition using threads, a process context switch would only occur when a thread from a different process is the next thread to be assigned the processor. Less overhead means less system resources used and less time taken for context switching. Of course, if there are enough processors to go around then context switching is not an issue.
220.127.116.11 Better Throughput
Multiple threads can increase the throughput of an application. With one thread, an I/O request would halt the entire process. With multiple threads, as one thread waits for an I/O request, the application can continue executing. As one thread is blocked, another can execute. The entire application does not wait for each I/O request to be filled. Other tasks can be performed that does not depend on the blocked thread.
18.104.22.168 Simpler Communication between Concurrently Executing Parts
Threads do not require special mechanisms for communication between subtasks. Threads can directly pass and receive data from other threads. This also saves system resources that would have to be used in the setup and maintenance of special communication mechanisms if multiple processes were used. Threads communicate by using the memory shared within the address space of the process. Processes can also communicate by shared memory but processes have separate address spaces and therefore the shared memory exists outside the address space of both processes. This increases the time and space used to maintain and access the shared memory. Figure 4-2 illustrates the communication between processes and threads.
Figure 4-2. Communication between threads of a single process and communication between multiple processes.
22.214.171.124 Simplify Program Structure
Threads can be used to simplify the program structure of an application. Each thread is assigned a subtask or subroutine for which it is responsible. The thread will independently manage the execution of the subtask. Each thread can be assigned a priority reflecting the importance of the subtask it is executing to the application. This will result in more easily maintained code.
4.1.4 Disadvantages of Threads
The easy accessibility threads have to the memory of a process has its disadvantages. For example:
Threads can easily pollute address space of a process.
Threads will require synchronization for concurrent read/write access to memory.
One thread can kill the entire process or program.
Threads only exist within a single process and are therefore not reusable.
126.96.36.199 Threads Can Corrupt Process Data Easier
It's easier for threads to corrupt the data of a process through data race because multiple threads have write access to the same piece of data. This is not so with processes. Each process has its own data and other processes don't have access unless special programming is done. The separate address spaces of processes protect the data. The fact that threads share the same address space exposes the data to corruption. For example, a process has three threads, A, B, and C. Threads A and B write to a memory location and Thread C reads the value and uses it in a calculation. Threads A and B may both attempt to write to the memory location at the same time. Thread B overwrites the data written by Thread A before Thread C gets a chance to read it. The threads need to be synchronized so that Thread C can read the data deposited in the memory location by Thread A before Thread B overwrites it. Synchronization is needed to prevent either thread from overwriting the values before the data is used. The issues of synchronization between threads will be discussed in Chapter 5.
188.8.131.52 One Bad Thread Can Kill the Entire Program
Since threads don't have their own address space, they are not isolated. If a thread causes a fatal access violation, this may result in the termination of the entire process. Processes are isolated. If one process corrupts its address space, the problems are restricted to that process. A process can have an access violation that causes the process to terminate and all of the other processes will continue executing if the violation isn't too bad. Data errors can be restricted to a single process. Errors caused by a thread are more costly than errors caused by processes. Threads can create data errors that affect the entire memory space of all the threads. Processes can protect its resources from indiscriminate access by other processes. Threads share resources with all the other threads in the process. A thread that damages a resource affects the whole process or program.
184.108.40.206 Threads are Not as Reusable by Other Programs
Threads are dependent and cannot be separated from the process in which they reside. Processes are more independent than threads. An application can divide tasks among many processes and those processes can be packaged as modules that can be used in other applications. Threads cannot exist outside the process that created it and therefore are not reusable. Table 4-2 lists the advantages and disadvantages of threads.
Table 4-2. Advantages and Disadvantages of Threads
Advantages of Threads
Disadvantages of Threads