C++ Multithreading Cookbook Over 60 recipes to help you create ultra-fast multithreaded applications using C++ with rules, guidelines, and best practices Miloš Ljumović BIRMINGHAM - MUMBAI www.it-ebooks.info
C++ Multithreading Cookbook Copyright © 2014 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: August 2014 Production reference: 1250714 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78328-979-0 www.packtpub.com Cover image by Radivoje Todorović (mr.todorovic@gmail.com) www.it-ebooks.info
Credits Author Miloš Ljumović Reviewers Abhishek Gupta Venkateshwaran Loganathan Walt Stoneburner Dinesh Subedi Commissioning Editor Edward Gordon Acquisition Editor Nikhil Karkal Content Development Editor Akshay Nair Technical Editors Tanvi Bhatt Faisal Siddiqui Copy Editors Janbal Dharmaraj Karuna Narayanan Sayanee Mukherjee Alfida Paiva Project Coordinator Mary Alex Proofreaders Simran Bhogal Kevin McGowan Chris Smith Indexers Hemangini Bari Mehreen Deshmukh Rekha Nair Graphics Disha Haria Production Coordinator Komal Ramchandani Cover Work Komal Ramchandani www.it-ebooks.info
About the Author Miloš Ljumović was born on July 26 in Podgorica, the capital of Montenegro in Europe, where he attended elementary and high school. He also went to music school to play the guitar. He studied Computer Science in the Faculty of Natural Science and Mathematics, Montenegro University. The following year, he picked up interest in operating systems, which he majored in, in his Master's degree. In December 2009, along with his friend Danijel, he started his company, Programmer, along with a highly qualified team to provide high-tech IT solutions. Soon, many skilled developers joined them, and from this collaboration, many applications and system software, web applications, and database systems have arisen. His clients were not only the Government of Montenegro, but also some major domestic companies. He developed a new age financial system for the national domain company, MeNet; he also developed video recognition software, along with pictures and other digital media types. He also developed many fascinating websites and other web applications. The list of customers is too long to be incorporated here. After working only for a few months as an international consultant, he worked with an American company in a project involving large e-commerce businesses and data scraping from the Internet. All this was the spark to open his new company, EXPERT.ITS.ME, in the beginning of July 2014. Besides development, he provides consulting services and helps small businesses in the IT sector to manage problems while inspiring them to be and think big. He is also a member of the Committee of National Chamber (ICT) of Montenegro and MENSA. He likes programming in C/C++/C# even though he is skilled in HTML, PHP, TSQL, and more while going toward his dream of developing his own operating system. In his spare time, he plays tennis, dives, hunts, or plays chess. He favors brainstorming with his team and fresh, modern ideas in the area of IT and computer science. He is continuously going towards new beginnings and next-generation software solutions. He specially likes teaching computer science and math students through private classes and courses and shaping them to be qualified programmers and helping them find all the beauty of science. To see what his interests are and what he is doing, visit his company's website (http://expert.its.me) or his website (http://milos.expert.its.me), or contact him at milos@expert.its.me. www.it-ebooks.info
Acknowledgments I am grateful to many for writing this book. I dedicate this book to my parents Radoslav and Slavka and to my sisters Natalija and Dušanka, who are always there for me no matter what. I am specially grateful to my mother, without whom I wouldn't became a programmer. An even greater thanks goes to my beautiful wife, Lara, for putting up with me, for all her patience and love towards me, for all her unconditional support, and for teaching me never to give up. Volim te. My appreciation goes to my dear friend Danijel who taught me how to be great businessman and pushed me over the edge to be better programmer each day. I would also like to thank my professors from the University of Montenegro, without whom I wouldn't be the expert I am today. A special thanks to Rajko Ćalasan for endless hours teaching me programming, Milo Tomašević for providing me with expertise in object-orienting programming, and making me love C++ the way I do today, and especially to Goran Šuković, the greatest teacher of all, for constantly guiding me and teaching me various areas of computer science and making me wish and get more and more knowledge each day. www.it-ebooks.info
About the Reviewers Abhishek Gupta is a young embedded software engineer from Bangalore, India. He has been working on software for Automotive In-Vehicle Infotainment (IVI) for the past few years. He completed his MTech from IIT Kharagpur, India in Visual Information and Embedded Systems in 2011. He is passionate about video processing and loves to work on embedded multimedia systems. His technical knowledge revolves mostly around C and Linux. You can find more information about him at www.abhitak.wordpress.com/about-me. Venkateshwaran Loganathan is an eminent software developer who has been involved in the design, development, and testing of software products for more than 6 years now. He was introduced to computer programming at an early age of 11 with FoxPro, and then started to learn and master various computer languages such as C, C++, Perl, Python, Node.js, and Unix shell scripting. Fascinated by open source development, he has involved himself in contributing to various Open Source technologies. He is now working for Cognizant Technology Solutions as an associate in technology, where he has involved himself in research and development for the Internet of Things domain. He is now actively involved in using RFID devices to evolve Future of Technology concepts. Before joining Cognizant, he had worked with some of the major IT firms such as Infosys, Virtusa, and NuVeda. Starting his career as a network developer, he has gained expertise in various domains such as networking, e-learning, and healthcare. He has won various awards and accolades in the companies he has worked for. He holds a Bachelor's degree in Computer Science and Engineering from Anna University and is currently pursuing an M.S. in Software Systems from BITS, Pilani. Apart from programming, he is actively involved in handling various technical and soft skills classes for budding engineers and college students. He also likes singing and trekking. He likes to get involved in social service and moves with people a lot. Visit him online at http://www.venkateshwaranloganathan.com and write to him at anandvenkat4@gmail.com. www.it-ebooks.info
He has also published the book PySide GUI Application Development, Packt Publishing. I am indebted to many. First of all, I would like to thank my mother, Anbuselvi, and grandmother, Saraswathi, for their endless effort and perseverance in bringing me up to this level. I would also like to thank all my friends and brothers; the list is too long to mention here. They all have been my well- wishers and have helped me in my tough times. I have not mentioned the names of many people here, but my thanks are always due to those who directly or indirectly influenced my life. Above all, thanks to the Almighty for the showering his blessings on me. Walt Stoneburner is a software architect with over 25 years of commercial application development and consulting experience. Fringe passions involve quality assurance, configuration management, and security. If cornered, he may actually admit to liking statistics and authoring documentation as well. He's easily amused by programming language design, collaborative applications, big data, knowledge management, data visualization, and ASCII art. Self-described as a closet geek, Walt also evaluates software products and consumer electronics, draws comics, runs a freelance photography studio specializing in portraits and art (CharismaticMoments.com), writes humor pieces, performs sleights of hand, enjoys game design, and can occasionally be found on ham radio. Walt may be reached directly via e-mail at wls@wwco.com or at Walt.Stoneburner@ gmail.com. He publishes a tech and humor blog called the Walt-O-Matic at http://www. wwco.com/~wls/blog/. www.it-ebooks.info
His other book reviews and contributions include: f AntiPatterns and Patterns in Software Configuration Management (ISBN 978-0-471-32929-9, p. xi) f Exploiting Software: How to Break Code (ISBN 978-0-201-78695-8, p. xxxiii) f Ruby on Rails: Web Mashup Projects (ISBN 978-1-847193-93-3) f Building Dynamic Web 2.0 Websites with Ruby on Rails (ISBN 978-1-847193-41-4) f Instant Sinatra Starter (ISBN 978-1782168218) f Learning Selenium Testing Tools with Python (978-1-78398-350-6) f Whittier (ASIN B00GTD1RBS) f Cooter Brown's South Mouth Book of Hillbilly Wisdom (ISBN 978-1-482340-99-0) Dinesh Subedi is a software developer at Yomari Incorporated Pvt. Ltd. He is currently working on data warehouse technology and business intelligence. He is a blogger at www. codeincodeblock.com writing articles related to software development using C++ and has four years experience with it. He has completed his B.E. in Computer Engineering form Pulchowk Campus IOE Kathmandu, Nepal. I would like to thank my brother Bharat Subedi who helped me while reviewing this book. www.it-ebooks.info
www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book. 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. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info
Table of Contents Preface 1 Chapter 1: Introduction to C++ Concepts and Features 7 Introduction 7 Creating a C++ project 8 Program structure, execution flow, and runtime objects 10 The structural programming approach 14 Understanding the object-oriented programming approach 17 Explaining inheritance, overloading, and overriding 19 Understanding polymorphism 24 Event handlers and Message Passing Interface 28 Linked list, queue, and stack examples 34 Chapter 2: The Concepts of Process and Thread 45 Introduction 45 Processes and threads 46 Explaining the process model 47 Implementation of processes 51 IPC – Interprocess Communication 55 Solving classical IPC problems 65 Implementation of the thread model 75 Thread usage 81 Implementing threads in user space 89 Implementing threads in the kernel 99 Chapter 3: Managing Threads 105 Introduction 105 Processes versus threads 106 Permissive versus preemptive multitasking 110 Explaining the Windows Thread object 111 Basic thread management 112 www.it-ebooks.info
ii Table of Contents Implementing threads without synchronization 121 Using synchronized threads 127 Win32 synchronization objects and techniques 133 Chapter 4: Message Passing 141 Introduction 141 Explaining the Message Passing Interface 142 Understanding a message queue 147 Using the thread message queue 156 Communicating through the pipe object 161 Chapter 5: Thread Synchronization and Concurrent Operations 167 Introduction 167 Pseudoparallelism 168 Understanding process and thread priority 168 The Windows dispatcher object and scheduling 176 Using mutex 177 Using semaphore 187 Using event 197 Using critical section 206 Using pipes 215 Chapter 6: Threads in the .NET Framework 233 Introduction 233 Managed versus unmanaged code 234 How threading works in .NET 235 The difference between foreground and background threads 243 Understanding the .NET synchronization essentials 247 Locking and avoiding deadlocks 254 Thread safety and types of the .NET framework 261 Signaling with event wait handles 263 Event-based Asynchronous Pattern 269 Using the BackgroundWorker class 276 Interrupting, aborting, and safe canceling the thread execution 281 Non-blocking synchronization 291 Signaling with Wait and Pulse 294 The Barrier class 299 www.it-ebooks.info
iii Table of Contents Chapter 7: Understanding Concurrent Code Design 309 Introduction 309 How to design parallel applications 310 Understanding parallelism in code design 316 Turning on to a parallel approach 324 Improving the performance factors 332 Chapter 8: Advanced Thread Management 341 Introduction 341 Using thread pools 342 Customizing the thread pool dispatcher 354 Using remote threading 371 Appendix 381 Installing MySQL Connector/C 381 Installing WinDDK – Driver Development Kit 384 Setting up a Visual Studio project for driver compilation 386 Using the DebugView application 392 Setting up a Visual Studio project for OpenMP compilation 393 Index 397 www.it-ebooks.info
Preface Creating multithreaded applications is a present-day approach towards programming. Developers expect their applications to be user friendly, with a rich interface and concurrent execution. The power of the C++ language alongside the native Win32 API features will give you a head start over all other languages and frameworks. With the power of C++, you can easily create various types of applications and perform parallelism and optimizations in your existing work. This book is a practical, powerful, and easy-to-understand guide to C++ multithreading. You will learn how to benefit from the multithreaded approach and enhance your development skills to build better applications. This book will not only help you avoid problems when creating parallel code, but also help you understand synchronization techniques in detail. The book also covers the Windows process model alongside scheduling techniques and Interprocess Communication. Starting from the basics, you will be introduced to the most powerful Integrated Development Environment ever made, that is Microsoft Visual Studio. You will then learn to use the native features of a Windows kernel as well as the characteristics of the .NET framework. You will then understand how to solve some common concurrent problems and learn how to properly think in a multithreaded environment. Using mutexes, semaphores, critical sections, monitors, events, and pipes, you will learn the most efficient way of synchronization in your C++ application. The book will teach you the best possible approach to learn concurrency in C++. Using the C++ native calls, the book will show you how to leverage machine hardware for optimum performance. The end goal of the book is to impart various multithreading concepts that will enable you to do parallel computing and concurrent programming quickly and efficiently. www.it-ebooks.info
Preface 2 What this book covers Chapter 1, Introduction to C++ Concepts and Features, introduces the C++ programming language along with its large number of features. It focuses on concepts such as the structure of the program, execution flow, and Windows OS runtime objects. The structural and object- oriented approach is also covered in detail. Chapter 2, The Concepts of Process and Thread, covers process and thread objects in detail. The idea behind the process model and implementation of Windows processes are covered thoroughly. We go through Interprocess Communication along with classical IPC problems. We then go through the overview of thread implementation in the user space as well as in the kernel. Chapter 3, Managing Threads, gives you the logic behind the process and the thread. We cover Windows OS features such as permissive and preemptive multitasking. We also cover thread synchronization and synchronization objects and techniques in detail. Chapter 4, Message Passing, focuses on message-passing techniques, window handlers, along with message queues and pipe communication. Chapter 5, Thread Synchronization and Concurrent Operations, talks about parallelism, priority, the dispatcher object, and scheduling techniques. We also explain synchronization objects such as mutex, semaphore, event, and critical section. Chapter 6, Threads in the .NET Framework, gives an overview of the C++/CLI .NET thread object. We briefly cover the Managed approach, .NET synchronization essentials, .NET thread safety, event-based asynchronous pattern, and the BackgroundWorker object along with some other topics. Chapter 7, Understanding Concurrent Code Design, covers features such as performance factors, correctness, and liveness problems. In this chapter, users can find a better perspective of concurrency and parallel application design. Chapter 8, Advanced Thread Management, focuses on a higher aspect of thread management. Abstractions of the thread pool, along with a custom dispatching object with deadlock resolution are covered in detail. Remote threading is given as a final example of advanced management. Appendix covers the Installation of MySQL Connector C and WinDDK—Driver Development Kit. Other than that it contains the setting Visual Studio project for driver compilation as well as for OpenMP compilation. It covers the installation of DebugView application and shows the steps to use it. www.it-ebooks.info
Preface 3 What you need for this book To execute the examples in the book, the following software will be required: f Visual Studio 2013 http://www.visualstudio.com/downloads/download-visual- studio-vs#d-express-windows-8 f Windows Drivers Kit: WinDDK http://msdn.microsoft.com/en-us/windows/hardware/ hh852365.aspx f MySQL Connector C http://dev.mysql.com/downloads/connector/c/ Who this book is for This book is intended primarily for intermediate and advanced users. Synchronization concepts are covered from the very beginning, thus making the book readable for all developers unassociated to any particular branch of expertise. The last two chapters will provide great knowledge to advanced users, providing an excellent overview on concepts such as concurrent design and advanced thread management. Conventions In this book, you will find a number of styles of text 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: "We can include other contexts through the use of the include directive." A block of code is set as follows: class CLock { public: CLock(TCHAR* szMutexName); ~CLock(); www.it-ebooks.info
Preface 4 private: HANDLE hMutex; }; inline CLock::CLock(TCHAR* szMutexName) { hMutex = CreateMutex(NULL, FALSE, szMutexName); WaitForSingleObject(hMutex, INFINITE); } inline CLock::~CLock() { ReleaseMutex(hMutex); CloseHandle(hMutex); } When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold: class CLock { public: CLock(TCHAR* szMutexName); ~CLock(); private: HANDLE hMutex; }; inline CLock::CLock(TCHAR* szMutexName) { hMutex = CreateMutex(NULL, FALSE, szMutexName); WaitForSingleObject(hMutex, INFINITE); } inline CLock::~CLock() { ReleaseMutex(hMutex); CloseHandle(hMutex); } It is very important is for reader to pay attention when copying and pasting code directly from the book. Due to book dimensions, some code lines could not be put into a single line. We tried to overcome this issue but simply it wasn't possible in certain situations. We strongly recommend you to check such cases before start compiling the examples. Especially in cases of double quoted strings, if in some accidental case the string is split as it should not be. www.it-ebooks.info
Preface 5 Also very important is a step in the "How to do it" sections when we say, for example, "Add existing header file CQueue.h previously implemented in Chapter 1, Introduction to C++ Concepts and Features". What we mean by this is that you should navigate to the folder where CQueue.h file resides using Windows Explorer and you should then copy the file along with all its dependences—in this case CList.h—to the example project working folder, before you add it to the project, using the "Add Existing Header File" option. By following this procedure you will be able to properly compile and run the example code. New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Open Solution Explorer and right-click on Header files." Warnings or important notes appear in a box 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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of. To send us general feedback, simply send an e-mail to feedback@packtpub.com, and mention the book title via 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 on www.packtpub.com/authors. Customer support Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase. Downloading the example code You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. www.it-ebooks.info
Comments 0
Loading comments...
Reply to Comment
Edit Comment