Concurrent Programming in C++
Introduction
This tutorial consists of two sections:
- C++11 Concurrency
- Intel Threaded Building Blocks
C++11 Concurrency
C++11 introduced direct support in the language for concurrency, with the ability to launch and manage different threads. Support for mutexes and other synchronisation mechanisms are available.
Threaded Building Blocks
Threaded Building Blocks is a higher level C++ that supports many common design patterns for applications to take advantage of multi-threading capabilities. Basic patterns like parallel loops and reductions are available. We'll also look at the TBB graph constructs.
TBB includes useful containers, which are thread safe, as well as a high performance multi-threaded implementation of malloc.
Setup
General
It's important to setup the Redhat devtoolset to have access to a modern version of gcc for all of the tutorial exercises.
To do this login and execute this command:
$ scl enable devtoolset-2 bash
You can verify that all is well by asking for the gcc version, which should be 4.8.2:
$ g++ --version
g++ (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
C++11 Compiler Options
To compile concurrent C++11 programs you'll need two flags for g++:
-std=c++11
- Use the new C++11 standards-pthread
- Enable posix thread support, which is the underlying thread library used by libstdc++ on linux platforms
You might well find this Makefile
useful. It will compile any .cc
file into an executable with the correct
compiler flags.
A good reference for C++ concurrency libraries is http://www.cplusplus.com/ (use the reference section, under multi-threading).
Threaded Building Blocks
Intel Threaded Building Blocks has been compiled and installed into
/usr/local
(specifically /usr/local/include
for header files and
/usr/local/lib64
for libraries). Thus you shouldn't need any special
extra setup to use is during the school. (Actually TBB is very easy to install from source.)
Documentation for TBB is installed into /usr/local/share/tbb/html
,
but using the
Intel documentation is
probably more convenient.
References
The current github version of this document is here:
https://github.com/graeme-a-stewart/cpp-concurrency/blob/master/doc/00-TipsReferences.md
and it also includes lists of useful references.