Statistics
2
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-04-21

AuthorMaya Posch

Key Features Delve into the fundamentals of multithreading and concurrency and find out how to implement them Explore atomic operations to optimize code performance Apply concurrency to both distributed computing and GPGPU processing Book Description Multithreaded applications execute multiple threads in a single processor environment, allowing developers achieve concurrency. This book will teach you the finer points of multithreading and concurrency concepts and how to apply them efficiently in C++. Divided into three modules, we start with a brief introduction to the fundamentals of multithreading and concurrency concepts. We then take an in-depth look at how these concepts work at the hardware-level as well as how both operating systems and frameworks use these low-level functions. In the next module, you will learn about the native multithreading and concurrency support available in C++ since the 2011 revision, synchronization and communication between threads, debugging concurrent C++ applications, and the best programming practices in C++. In the final module, you will learn about atomic operations before moving on to apply concurrency to distributed and GPGPU-based processing. The comprehensive coverage of essential multithreading concepts means you will be able to efficiently apply multithreading concepts while coding in C++. What you will learn Deep dive into the details of the how various operating systems currently implement multithreading Choose the best multithreading APIs when designing a new application Explore the use of mutexes, spin-locks, and other synchronization concepts and see how to safely pass data between threads Understand the level of API support provided by various C++ toolchains Resolve common issues in multithreaded code and recognize common pitfalls using tools such as Memcheck, CacheGrind, DRD, Helgrind, and more Discover the nature of atomic operations and understand how they can be useful in optimizing code Implement a multithr

Tags
No tags
ISBN: 1787121704
Publisher: Packt Publishing
Publish Year: 2016
Language: 英文
Pages: 244
File Format: PDF
File Size: 5.8 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Mastering C++ Multithreading A comprehensive guide to developing effective multithreading applications in C++ Maya Posch BIRMINGHAM - MUMBAI
Mastering C++ Multithreading Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: July 2017 Production reference: 1270717 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78712-170-6 www.packtpub.com
Credits Author Maya Posch Copy Editor Sonia Mathur Reviewer Louis E. Mauget Project Coordinator Vaidehi Sawant Commissioning Editor Aaron Lazar Proofreader Safis Editing Acquisition Editor Chaitanya Nair Indexer Francy Puthiry Content Development Editor Rohit Kumar Singh Graphics Abhinash Sahu Technical Editors Ketan Kamble Production Coordinator Nilesh Mohite
About the Author Maya Posch is a software engineer by trade and a self-professed electronics, robotics, and AI nut, running her own software development company, Nyanko, with her good friend, Trevor Purdy, where she works on various game development projects and some non-game projects. Apart from this, she does various freelance jobs for companies around the globe. You can visit her LinkedIn profile for more work-related details. Aside from writing software, she likes to play with equations and write novels, such as her awesome reimagining of the story of the Nintendo classic, Legend of Zelda: Ocarina of Time, and the survival-horror novel she recently started, Viral Desire. You can check out her Scribd profile for a full listing of her writings. Maya is also interested in biochemistry, robotics, and reverse-engineering of the human body. To know more about her, visit her blog, Artificial Human. If there's anything she doesn't lack, it has to be sheer ambition, it seems.
About the Reviewer Louis E. Mauget learned to program a long time ago at the Michigan State University as a physics major learning to use software in designing a cyclotron. Later, he worked for 34 years at IBM. He went on to work for several consulting firms, including a long-term engagement with the railroad industry. He is currently consulting for Keyhole Software at Leawood, Kansas. Lou has coded in C++, Java, JavaScript, Python, and newer languages, as each was conceived. His current interests include reactive functional programming, containers, Node JS, NoSQL, geospatial systems, mobile, and so on, in any new language or framework. He occasionally blogs about software technology for Keyhole Software. He has coauthored three computer books and authored two IBM DeveloperWorks XML tutorials and a WebSphere Journal LDAP tutorial. Lou co-wrote several J2EE certification tests for IBM. He has also worked as a reviewer for Packt Publishing and others.
www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. h t t p s ://w w w . p a c k t p u b . c o m /m a p t Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser
Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at h t t p s ://w w w . a m a z o n . c o m /d p /1787121704. If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Table of Contents Preface 1 Chapter 1: Revisiting Multithreading 6 Getting started 6 The multithreaded application 7 Makefile 11 Other applications 13 Summary 14 Chapter 2: Multithreading Implementation on the Processor and OS 15 Defining processes and threads 16 Tasks in x86 (32-bit and 64-bit) 18 Process state in ARM 21 The stack 22 Defining multithreading 23 Flynn's taxonomy 25 Symmetric versus asymmetric multiprocessing 26 Loosely and tightly coupled multiprocessing 27 Combining multiprocessing with multithreading 27 Multithreading types 27 Temporal multithreading 27 Simultaneous multithreading (SMT) 28 Schedulers 28 Tracing the demo application 30 Mutual exclusion implementations 32 Hardware 33 Software 33 Summary 35 Chapter 3: C++ Multithreading APIs 36 API overview 36 POSIX threads 37 Windows support 39 PThreads thread management 40 Mutexes 42 Condition variables 43
[ ii ] Synchronization 45 Semaphores 46 Thread local storage (TLC) 46 Windows threads 47 Thread management 48 Advanced management 50 Synchronization 51 Condition variables 51 Thread local storage 52 Boost 52 Qt 52 QThread 53 Thread pools 54 Synchronization 54 QtConcurrent 55 Thread local storage 55 POCO 55 Thread class 56 Thread pool 56 Thread local storage (TLS) 57 Synchronization 58 C++ threads 59 Putting it together 59 Summary 60 Chapter 4: Thread Synchronization and Communication 61 Safety first 61 The scheduler 62 High-level view 62 Implementation 63 Request class 65 Worker class 67 Dispatcher 69 Makefile 73 Output 74 Sharing data 77 Using r/w-locks 78 Using shared pointers 78 Summary 78 Chapter 5: Native C++ Threads and Primitives 79
[ iii ] The STL threading API 79 Boost.Thread API 79 The 2011 standard 80 C++14 81 C++17 81 STL organization 82 Thread class 83 Basic use 83 Passing parameters 84 Return value 85 Moving threads 85 Thread ID 86 Sleeping 87 Yield 88 Detach 88 Swap 88 Mutex 89 Basic use 89 Non-blocking locking 91 Timed mutex 92 Lock guard 93 Unique lock 94 Scoped lock 95 Recursive mutex 95 Recursive timed mutex 96 Shared mutex 96 Shared timed mutex 97 Condition variable 97 Condition_variable_any 100 Notify all at thread exit 100 Future 101 Promise 102 Shared future 103 Packaged_task 104 Async 105 Launch policy 106 Atomics 106 Summary 106 Chapter 6: Debugging Multithreaded Code 107
[ iv ] When to start debugging 107 The humble debugger 108 GDB 109 Debugging multithreaded code 110 Breakpoints 111 Back traces 112 Dynamic analysis tools 114 Limitations 115 Alternatives 115 Memcheck 116 Basic use 116 Error types 119 Illegal read / illegal write errors 119 Use of uninitialized values 119 Uninitialized or unaddressable system call values 121 Illegal frees 123 Mismatched deallocation 123 Overlapping source and destination 123 Fishy argument values 124 Memory leak detection 124 Helgrind 125 Basic use 125 Misuse of the pthreads API 130 Lock order problems 131 Data races 132 DRD 132 Basic use 132 Features 134 C++11 threads support 135 Summary 136 Chapter 7: Best Practices 137 Proper multithreading 137 Wrongful expectations - deadlocks 138 Being careless - data races 142 Mutexes aren't magic 147 Locks are fancy mutexes 149 Threads versus the future 150 Static order of initialization 150 Summary 153 Chapter 8: Atomic Operations - Working with the Hardware 154
[ v ] Atomic operations 154 Visual C++ 155 GCC 161 Memory order 164 Other compilers 165 C++11 atomics 165 Example 168 Non-class functions 169 Example 170 Atomic flag 172 Memory order 172 Relaxed ordering 173 Release-acquire ordering 173 Release-consume ordering 174 Sequentially-consistent ordering 174 Volatile keyword 175 Summary 175 Chapter 9: Multithreading with Distributed Computing 176 Distributed computing, in a nutshell 176 MPI 178 Implementations 179 Using MPI 180 Compiling MPI applications 181 The cluster hardware 182 Installing Open MPI 186 Linux and BSDs 186 Windows 186 Distributing jobs across nodes 188 Setting up an MPI node 189 Creating the MPI host file 189 Running the job 190 Using a cluster scheduler 190 MPI communication 191 MPI data types 192 Custom types 193 Basic communication 195 Advanced communication 196 Broadcasting 196 Scattering and gathering 197 MPI versus threads 198
[ vi ] Potential issues 200 Summary 200 Chapter 10: Multithreading with GPGPU 201 The GPGPU processing model 201 Implementations 202 OpenCL 203 Common OpenCL applications 203 OpenCL versions 204 OpenCL 1.0 204 OpenCL 1.1 204 OpenCL 1.2 205 OpenCL 2.0 206 OpenCL 2.1 206 OpenCL 2.2 207 Setting up a development environment 208 Linux 208 Windows 208 OS X/MacOS 209 A basic OpenCL application 209 GPU memory management 213 GPGPU and multithreading 215 Latency 216 Potential issues 216 Debugging GPGPU applications 217 Summary 218 Index 219
Preface Multithreaded applications execute multiple threads in a single processor environment, to achieve. Filled with practical examples, this book will help you become a master at writing robust concurrent and parallel applications in C++. In this book, you will delve into the fundamentals of multithreading and concurrency and find out how to implement them. While doing so, you will explore atomic operations to optimize code performance and also apply concurrency to both distributed computing and GPGPU processing. What this book covers Chapter 1, Revisiting Multithreading, summarizes multithreading in C++, revisiting all the concepts you should already be familiar with and going through a basic example of multithreading using the native threading support added in the 2011 revision of C++. Chapter 2, Multithreading Implementation on the Processor and OS, builds upon the fundamentals provided by the hardware implementations discussed in the preceding chapter, showing how OSes have used the capabilities to their advantage and made them available to applications. It also discusses how processes and threads are allowed to use the memory and processor in order to prevent applications and threads from interfering with each other. Chapter 3, C++ Multithreading APIs, explores the wide variety of multithreading APIs available as OS-level APIs (for example, Win32 and POSIX) or as provided by a framework (for example, Boost, Qt, and POCO). It briefly runs through each API, listing the differences compared to the others as well as the advantages and disadvantages it may have for your application. Chapter 4, Thread Synchronization and Communication, takes the topics learned in the previous chapters and explores an advanced multithreading implementation implemented using C++ 14's native threading API, which allows multiple threads to communicate without any thread-safety issues. It also covers the differences between the many types of synchronization mechanisms, including mutexes, locks, and condition variables. Chapter 5, Native C++ Threads and Primitives, includes threads, concurrency, local storage, as well as thread-safety supported by this API. Building upon the example in the preceding chapter, it discusses and explores extending and optimizing thread-safty using the features offered by the full feature set in C++ 11 and C++ 14.
Preface [ 2 ] Chapter 6, Debugging Multithreaded Code, teaches you how to use tools such as Valgrind (Memcheck, DRD, Helgrind, and so on) to analyze the multithreaded performance of an application, find hotspots, and resolve or prevent issues resulting from concurrent access. Chapter 7, Best Practices, covers common pitfalls and gotchas and how to spot them before they come back to haunt you. It also explores a number of common and less common scenarios using examples. Chapter 8, Atomic Operations – Working with the Hardware, covers atomic operations in detail: what they are and how they are best used. Compiler support is looked at across CPU architectures and an evaluation is made of when it is worth to invest time in implementing atomic operations in your code. It also looks at how such optimizations will limit the portability of your code. Chapter 9, Multithreading with Distributed Computing, takes many of the lessons learned in the preceding chapters and applies them on a multi-system, cluster-level scale. Using an OpenMPI-based example, it shows how multithreading can be done across multiple systems, such as the nodes in a computer cluster. Chapter 10, Multithreading with GPGPU, shows the use of multithreading in GPGPU applications (for example, CUDA and OpenCL). Using an OpenCL-based example, a basic multithreaded application is explored that can execute tasks in parallel. This chapter takes lessons learned in the preceding chapters and applies them to processing on video cards and derived hardware (for example, rack-mounted vector processor hardware). What you need for this book To follow the instructions in this book, you will need any OS (Windows, Linux, or macOS) and any C++ compiler installed on your systems. Who this book is for This book is for intermediate C++ developers who wish to extend their knowledge of multithreading and concurrent processing. You should have basic experience with multithreading and be comfortable using C++ development toolchains on the command line.
Preface [ 3 ] Conventions In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The randGen() method takes two parameters, defining the range of the returned value:" A block of code is set as follows: cout_mtx.lock(); cout << "Thread " << tid << " adding " << rval << ". New value: " << val << ".\n"; cout_mtx.unlock(); values_mtx.lock(); values.push_back(val); values_mtx.unlock(); } When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold: cout_mtx.lock(); cout << "Thread " << tid << " adding " << rval << ". New value: " << val << ".\n"; cout_mtx.unlock(); values_mtx.lock(); values.push_back(val); values_mtx.unlock(); } Any command-line input or output is written as follows: $ make g++ -o ch01_mt_example -std=c++11 ch01_mt_example.cpp
Preface [ 4 ] New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text. Warnings or important notes appear like this. Tips and tricks appear like this. Reader feedback Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail feedback@packtpub.com, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors. Downloading the example code You can download the example code files for this book from your account at h t t p ://w w w . p a c k t p u b . c o m . If you purchased this book elsewhere, you can visit h t t p ://w w w . p a c k t p u b . c o m /s u p p o r t and register to have the files e-mailed directly to you. You can download the code files by following these steps: Log in or register to our website using your e-mail address and password.1. Hover the mouse pointer on the SUPPORT tab at the top.2. Click on Code Downloads & Errata.3. Enter the name of the book in the Search box.4. Select the book for which you're looking to download the code files.5. Choose from the drop-down menu where you purchased this book from.6. Click on Code Download.7.
Preface [ 5 ] Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of: WinRAR / 7-Zip for Windows Zipeg / iZip / UnRarX for Mac 7-Zip / PeaZip for Linux The code bundle for the book is also hosted on GitHub at h t t p s ://g i t h u b . c o m /P a c k t P u b l i s h i n g /M a s t e r i n g - C P P - M u l t i t h r e a d i n g . We also have other code bundles from our rich catalog of books and videos available at h t t p s ://g i t h u b . c o m /P a c k t P u b l i s h i n g /. Check them out! Errata Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code- we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting h t t p ://w w w . p a c k t p u b . c o m /s u b m i t - e r r a t a , selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to h t t p s ://w w w . p a c k t p u b . c o m /b o o k s /c o n t e n t /s u p p o r t and enter the name of the book in the search field. The required information will appear under the Errata section. Piracy Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at copyright@packtpub.com with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content. Questions If you have a problem with any aspect of this book, you can contact us at questions@packtpub.com, and we will do our best to address the problem.
1 Revisiting Multithreading Chances are that if you're reading this book, you have already done some multithreaded programming in C++, or, possibly, other languages. This chapter is meant to recap the topic purely from a C++ point of view, going through a basic multithreaded application, while also covering the tools we'll be using throughout the book. At the end of this chapter, you will have all the knowledge and information needed to proceed with the further chapters. Topics covered in this chapter include the following: Basic multithreading in C++ using the native API Writing basic makefiles and usage of GCC/MinGW Compiling a program using make and executing it on the command-line Getting started During the course of this book, we'll be assuming the use of a GCC-based toolchain (GCC or MinGW on Windows). If you wish to use alternative toolchains (clang, MSVC, ICC, and so on), please consult the documentation provided with these for compatible commands. To compile the examples provided in this book, makefiles will be used. For those unfamiliar with makefiles, they are a simple but powerful text-based format used with the make tool for automating build tasks including compiling source code and adjusting the build environment. First released in 1977, make remains among the most popular build automation tools today. Familiarity with the command line (Bash or equivalent) is assumed, with MSYS2 (Bash on Windows) recommended for those using Windows.