Home > Articles

Dividing C++ Programs into Multiple Threads

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. This sample chapter will teach you how to "think multitasking" -- and improve your apps in the process.
This chapter is from the book

"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

  • Thread Scheduling

  • Thread Resources

  • Thread Models

  • Introduction to the Pthread Library

  • The Anatomy of a Simple Threaded Program

  • Creating Threads

  • Managing Threads

  • Thread Safety and Libraries

  • Dividing Your Program into Multiple Threads

  • Summary

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.

04fig01.gifFigure 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.

4.1.2.1 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.

4.1.2.2 Threads Controlling Other Threads

Whereas processes can only exercise control over other processes in which it has a parent–child 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 process—killing 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.

  • 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.

4.1.3.1 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.

4.1.3.2 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.

4.1.3.3 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.

04fig02.gifFigure 4-2. Communication between threads of a single process and communication between multiple processes.

4.1.3.4 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.

4.1.4.1 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.

4.1.4.2 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.

4.1.4.3 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

  • Less system resources needed for context switching

  • Increased throughput of an application

  • No special mechanism required for communication between tasks

  • Simplification of program structure

  • Require synchronization for concurrent read/write access to memory

  • Can easily pollute address space of its process

  • Only exist within a single process and therefore not reusable

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020