logo资料库

Mastering C++ Multithreading.pdf

第1页 / 共237页
第2页 / 共237页
第3页 / 共237页
第4页 / 共237页
第5页 / 共237页
第6页 / 共237页
第7页 / 共237页
第8页 / 共237页
资料共237页,剩余部分请下载后查看
Cover
Copyright
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Table of Contents
Preface
Chapter 1: Revisiting Multithreading
Getting started
The multithreaded application
Makefile
Other applications
Summary
Chapter 2: Multithreading Implementation on the Processor and OS
Defining processes and threads
Tasks in x86 (32-bit and 64-bit)
Process state in ARM
The stack
Defining multithreading
Flynn's taxonomy
Symmetric versus asymmetric multiprocessing
Loosely and tightly coupled multiprocessing
Combining multiprocessing with multithreading
Multithreading types
Temporal multithreading
Simultaneous multithreading (SMT)
Schedulers
Tracing the demo application
Mutual exclusion implementations
Hardware
Software
Summary
Chapter 3: C++ Multithreading APIs
API overview
POSIX threads
Windows support
PThreads thread management
Mutexes
Condition variables
Synchronization
Semaphores
Thread local storage (TLC)
Windows threads
Thread management
Advanced management
Synchronization
Condition variables
Thread local storage
Boost
Qt
QThread
Thread pools
Synchronization
QtConcurrent
Thread local storage
POCO
Thread class
Thread pool
Thread local storage (TLS)
Synchronization
C++ threads
Putting it together
Summary
Chapter 4: Thread Synchronization and Communication
Safety first
The scheduler
High-level view
Implementation
Request class
Worker class
Dispatcher
Makefile
Output
Sharing data
Using r/w-locks
Using shared pointers
Summary
Chapter 5: Native C++ Threads and Primitives
The STL threading API
Boost.Thread API
The 2011 standard
C++14
C++17
STL organization
Thread class
Basic use
Passing parameters
Return value
Moving threads
Thread ID
Sleeping
Yield
Detach
Swap
Mutex
Basic use
Non-blocking locking
Timed mutex
Lock guard
Unique lock
Scoped lock
Recursive mutex
Recursive timed mutex
Shared mutex
Shared timed mutex
Condition variable
Condition_variable_any
Notify all at thread exit
Future
Promise
Shared future
Packaged_task
Async
Launch policy
Atomics
Summary
Chapter 6: Debugging Multithreaded Code
When to start debugging
The humble debugger
GDB
Debugging multithreaded code
Breakpoints
Back traces
Dynamic analysis tools
Limitations
Alternatives
Memcheck
Basic use
Error types
Illegal read / illegal write errors
Use of uninitialized values
Uninitialized or unaddressable system call values
Illegal frees
Mismatched deallocation
Overlapping source and destination
Fishy argument values
Memory leak detection
Helgrind
Basic use
Misuse of the pthreads API
Lock order problems
Data races
DRD
Basic use
Features
C++11 threads support
Summary
Chapter 7: Best Practices
Proper multithreading
Wrongful expectations - deadlocks
Being careless - data races
Mutexes aren't magic
Locks are fancy mutexes
Threads versus the future
Static order of initialization
Summary
Chapter 8: Atomic Operations - Working with the Hardware
Atomic operations
Visual C++
GCC
Memory order
Other compilers
C++11 atomics
Example
Non-class functions
Example
Atomic flag
Memory order
Relaxed ordering
Release-acquire ordering
Release-consume ordering
Sequentially-consistent ordering
Volatile keyword
Summary
Chapter 9: Multithreading with Distributed Computing
Distributed computing, in a nutshell
MPI
Implementations
Using MPI
Compiling MPI applications
The cluster hardware
Installing Open MPI
Linux and BSDs
Windows
Distributing jobs across nodes
Setting up an MPI node
Creating the MPI host file
Running the job
Using a cluster scheduler
MPI communication
MPI data types
Custom types
Basic communication
Advanced communication
Broadcasting
Scattering and gathering
MPI versus threads
Potential issues
Summary
Chapter 10: Multithreading with GPGPU
The GPGPU processing model
Implementations
OpenCL
Common OpenCL applications
OpenCL versions
OpenCL 1.0
OpenCL 1.1
OpenCL 1.2
OpenCL 2.0
OpenCL 2.1
OpenCL 2.2
Setting up a development environment
Linux
Windows
OS X/MacOS
A basic OpenCL application
GPU memory management
GPGPU and multithreading
Latency
Potential issues
Debugging GPGPU applications
Summary
Index
Mastering C++ Multithreading Write robust, concurrent, and parallel applications 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 Reviewer Louis E. Mauget Copy Editor Sonia Mathur 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!
分享到:
收藏