You are previewing C++ Multithreading Cookbook.
O'Reilly logo
C++ Multithreading Cookbook

Book Description

Over 60 recipes to help you create ultra-fast multithreaded applications using C++ with rules, guidelines, and best practices

In Detail

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

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 to understand synchronization techniques in detail. The book also covers the Windows process model alongside scheduling techniques and interprocess communication.

Beginning from the basics, you will be introduced to the most powerful Integrated Development Environments ever made: Microsoft Visual Studio. You will then learn to use Windows kernel native features, as well as the characteristics of .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 & pipes, you will learn the most efficient way of synchronization in your C++ application. The book will then 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 will be to impart various multithreading concepts that will enable you to do parallel computing and concurrent programming quickly and efficiently.

What You Will Learn

  • Use an object-oriented programming model with inheritance, overloading, and polymorphism
  • Solve common Interprocess Communication problems and avoid deadlocks or starvation problems in your application development
  • Manage threads efficiently using the CThread class
  • Explore .NET CLI/C++ features as well as synchronization objects and techniques
  • Make use of parallel techniques in code design
  • Use machine resources in concurrent execution
  • Enable programs to work with each other using Message Passing
  • Avoid classic synchronization problems
  • Downloading the example code for this book. 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.

    Table of Contents

    1. C++ Multithreading Cookbook
      1. Table of Contents
      2. C++ Multithreading Cookbook
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. 1. Introduction to C++ Concepts and Features
        1. Introduction
        2. Creating a C++ project
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Program structure, execution flow, and runtime objects
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. The structural programming approach
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        5. Understanding the object-oriented programming approach
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Explaining inheritance, overloading, and overriding
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Understanding polymorphism
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Event handlers and Message Passing Interface
          1. Getting ready
          2. How to do it...
        9. Linked list, queue, and stack examples
          1. Getting ready
          2. How to do it...
          3. How it works...
      10. 2. The Concepts of Process and Thread
        1. Introduction
        2. Processes and threads
        3. Explaining the process model
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Implementation of processes
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. IPC – Interprocess Communication
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Solving classical IPC problems
          1. The dining philosophers problem
          2. Getting ready
          3. How to do it...
          4. How it works...
          5. See also...
        7. Implementation of the thread model
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Thread usage
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Implementing threads in user space
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Implementing threads in the kernel
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      11. 3. Managing Threads
        1. Introduction
        2. Processes versus threads
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Permissive versus preemptive multitasking
        4. Explaining the Windows Thread object
        5. Basic thread management
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Implementing threads without synchronization
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Using synchronized threads
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Win32 synchronization objects and techniques
          1. Synchronization object – mutex
          2. Synchronization object – semaphore
          3. Synchronization object – event
          4. Synchronization object – critical section
      12. 4. Message Passing
        1. Introduction
        2. Explaining the Message Passing Interface
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Understanding a message queue
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Using the thread message queue
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Communicating through the pipe object
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      13. 5. Thread Synchronization and Concurrent Operations
        1. Introduction
        2. Pseudoparallelism
        3. Understanding process and thread priority
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. The Windows dispatcher object and scheduling
        5. Using mutex
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Using semaphore
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Using event
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Using critical section
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Using pipes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      14. 6. Threads in the .NET Framework
        1. Introduction
        2. Managed versus unmanaged code
        3. How threading works in .NET
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. The difference between foreground and background threads
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Understanding the .NET synchronization essentials
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Locking and avoiding deadlocks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Thread safety and types of the .NET framework
        8. Signaling with event wait handles
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Event-based Asynchronous Pattern
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Using the BackgroundWorker class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Interrupting, aborting, and safe canceling the thread execution
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Safe cancelation
          4. Getting ready
          5. How to do it...
          6. How it works...
          7. There's more...
        12. Non-blocking synchronization
        13. Signaling with Wait and Pulse
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        14. The Barrier class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      15. 7. Understanding Concurrent Code Design
        1. Introduction
        2. How to design parallel applications
        3. Understanding parallelism in code design
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Turning on to a parallel approach
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Improving the performance factors
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      16. 8. Advanced Thread Management
        1. Introduction
        2. Using thread pools
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Customizing the thread pool dispatcher
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Using remote threading
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also...
      17. A. Appendix
        1. Installing MySQL Connector/C
        2. Installing WinDDK – Driver Development Kit
        3. Setting up a Visual Studio project for driver compilation
        4. Using the DebugView application
        5. Setting up a Visual Studio project for OpenMP compilation
      18. Index