Professional C__ - Marc Gregoire [384]
Multithreaded programming is a complicated subject. This chapter introduces you to multithreaded programming using the new C++11 library, but it cannot go into all details due to space constraints. There are entire books written about developing multithreaded programs. If you are interested in more details, consult one of the references in the multithreaded section in Appendix B.
If your compiler does not yet support the C++11 threading library, you might use other third-party libraries available that try to make multithreaded programming more platform independent. There are, for example, the pthreads library and the boost::thread library. However, since they are not part of the C++ standard, these are not discussed in this book.
INTRODUCTION
Multithreaded programming allows you to perform multiple calculations in parallel. This way you can take advantage of the multiple processors and cores inside most CPUs these days. Years ago, the CPU market was racing for the highest frequency, which is perfect for single threaded applications. This race has stopped due to a combination of power management and heat management problems. Today the CPU market is racing toward the most cores on a single CPU. Dual- and quad-core CPUs are already common at the time of this writing, and announcements have already been made about 12-, 16-, 32-, and even 80-core CPUs. They are still in an experimental stage, but you can be sure they will come out sooner than you might expect.
Similarly, if you look at the processing units on graphics cards, called GPUs, you’ll see that they are massively parallel computing units. Today, high-end graphics cards already have more than 2,000 cores and this will only increase rapidly. These graphics cards are not only used for gaming anymore, but also to perform computationally intense tasks. Examples are image and video manipulation, protein folding (useful for discovering new drugs), processing signals as part of the SETI project (Search for Extra-Terrestrial Intelligence), and so on.
C++98/03 did not have support for multithreaded programming, and you had to resort to third-party libraries or to the multithreading APIs of your target operating system. C++11 now includes a multithreading library, making it easier to write cross-platform multithreaded applications. There are two reasons to start writing multithreaded code. First, if you have a computational problem and you manage to separate it into small pieces that can be run in parallel independently from each other, you can expect a huge performance boost running it on modern multi-core GPUs and CPUs. Second, you can modularize computations along orthogonal axes; for example, doing long computations in a thread instead of blocking the GUI thread, so the user interface remains responsive while a long computation occurs in the background.
Figure 22-1 shows an example of a problem perfectly suited to run in parallel. An example could be the processing of pixels of an image by an algorithm that does not require information about neighboring pixels. The algorithm could split the image into four parts. On a single-core CPU, each part is processed sequentially; on a dual-core CPU, two parts are processed in parallel; and on a quad-core CPU, four parts are processed in parallel resulting in an almost linear scaling of the performance with the number of cores.
FIGURE 22-1
Of course, it’s not always possible to split the problem into parts that can be executed independently of each other in parallel. But often