Enterprise development and networking specialist Stephen B. Morris describes how to come to grips with the multithreaded features now available in C++11.
Do you think C++ multithreaded programming is simply too difficult for mere mortals? Or have your multithreaded programming efforts resulted in very difficult race condition problems? It doesn't have to be so hard; if you adhere to some solid design principles, you can reap the rewards of effective multithreaded coding. Disciplined use of multithreading can produce highly responsive programs with no need to worry about irreproducible race conditions or lost data updates. Indeed, the increased availability of multicore systems now makes knowledge of multithreaded development mandatory.
All languages change over time, and programming languages are no exception. In general, programming languages can be augmented by the use of external libraries, which has the effect of introducing a number of new symbols into the language. Another language-evolution mechanism is change to the core language itselfan event that occurs relatively rarely. The C++11 language change is an example of a core language modification.
Even though a core language change may require learning new structures and mechanisms, it's an evolution of the underlying language. Learning the new parts may be a bit of a pain, but should ultimately help us as programmers to become more productive. Core language changes also help to reposition a programming language in the modern world, keeping our work current and in demand.
C++11 is a backwardly compatible upgrade to the core of the C++ language. In other words, your existing C++ code should continue to work with no required changes. It wouldn't be good if all the millions of lines of C++ in common use had to be changed to work with C++11! In this article, we'll look at the new multithreading facilities in C++11, specifically related to general code design and deployment on multicore platforms.
Prior to C++11, multithreaded C++ required the use of platform-specific tools. This reality necessarily tied the solution to the platform of choiceWindows, Linux, Mac OS X, and so on. Being tied to a particular platform makes future code migration much more difficult. Imagine trying to move a Windows-based C++ application to the iPhone family or to the Android phone. It would probably be easier to start from scratch. A decade ago, the range of platforms was relatively modesta situation that's no longer the case. Therefore, being able to migrate C++ code to new platforms is increasingly important.
Let's have a look at some of the typical elements of a simple C++11 multithreaded application.