16
©Manning Publications Co. Please post comments or corrections to the Author Online forum:
http://www.manning-sandbox.com/forum.jspa?forumID=437
The Standard C++ Library also provides higher level abstractions and facilities that
make writing multi-threaded code easier and less error-prone. Sometimes the use of these
facilities does comes with a performance cost due to the additional code that must be
executed. However, this performance cost does not necessarily imply a higher abstraction
penalty though: in general the cost is no higher than would be incurred by writing equivalent
functionality by hand, and the compiler may well inline much of the additional code anyway.
In some cases, the high level facilities provide additional functionality beyond what may
be required for a specific use. Most of the time this is not an issue: you don't pay for what
you don't use. On rare occasions this unused functionality will impact the performance of
other code. If you are aiming for performance, and the cost is too high, you may be better
off hand-crafting the desired functionality from lower-level facilities. In the vast majority of
cases, the additional complexity and chance of errors far outweighs the potential benefits
from a small performance gain. Even if profiling does demonstrate that the bottleneck is in
the C++ Standard Library facilities, it may be due to poor application design rather than a
poor library implementation. For example, if too many threads are competing for a mutex it
will impact the performance significantly. Rather than trying to shave a small fraction of time
off the mutex operations, it would probably be more beneficial to restructure the application
so that there was less contention on the mutex. This sort of issue is covered in chapter 8.
In those very rare cases where the C++ Standard Library does not provide the
performance or behaviour required, it might be necessary to use platform specific facilities.
1.3.4 Platform-Specific Facilities
Whilst the C++ Thread Library provides reasonably comprehensive facilities for multi-
threading and concurrency, on any given platform there will be platform-specific facilities
that go beyond what is offered. In order to gain easy access to those facilities without giving
up the benefits of using the Standard C++ thread library, the types in the C++ Thread
Library may offer a
native_handle() member function which allows the underlying
implementation to be directly manipulated using a platform-specific API. By its very nature,
any operations performed using the
native_handle() are entirely platform dependent, and
out of the scope of this book (and the C++ Standard Library itself).
Of course, before even considering using platform-specific facilities, it's important to
understand what the Standard library provides, so let's get started with an example.
1.4 Getting Started
OK, so you've got a nice shiny C++09-compatible compiler. What next? What does a multi-
threaded C++ program look like? It looks pretty much like any other C++ program, with the
usual mix of variables, classes and functions. The only real distinction is that some functions
Licensed to JEROME RAYMOND <pedbro@gmail.com>