Book Image

C++ Multithreading Cookbook

By : Miloš Ljumović
Book Image

C++ Multithreading Cookbook

By: Miloš Ljumović

Overview of this book

<p>Creating multithreaded applications is a present-day approach towards programming. 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 to understand synchronization techniques. 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.</p>
Table of Contents (17 chapters)
C++ Multithreading Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using thread pools


Here, we will explain a thread pool, which can be represented as a list of worker threads that, when available, will execute the required tasks. This is good when an application wants to reduce the possibly high number of threads and provide management for working threads. Another scenario where the application should benefit is when there is a large number of threads responsible for small tasks. In such situations, the overhead for creation and destruction of a large number of threads is significant, and should be avoided with a small number of threads that will execute the entire work.

We see a typical thread pool implementation in the following diagram:

We will follow this pattern when designing the ThreadPool class. We'll use a list of threads that will be available for dispatching. In other words, the application will ask for a worker from the pool instead of creating threads. The pool manager—Dispatcher—will manage and schedule workers upon a request from an application...