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

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.

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.

What you need for this book

To execute the examples in the book, the following software will be required:

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();
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.

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

Note

Warnings or important notes appear in a box like this.

Tip

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

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 would 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 http://www.packtpub.com/submit-errata, 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.