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.
Talk
The introductory talk to this tutorial is here.
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.)
Compiling against TBB should just work, but linking requires -ltbb
and -lrt
.
The Makefile
here will add all the flags you need, and enable -std=c++11
so it's recommended.
Documentation for TBB is installed into /usr/local/share/tbb/html
,
but using the
Intel documentation is
more convenient.
Writing Code
You will have been given access to a machine on which to complete the tutorial. There are a suite of editors available and you can pick whichever you like best:
- nano - A very simple text editor (but lacks helpful things such as automatic indentation and code colourisation)
- emacs - The classic Stallman text editor
- vi - The other classic editor from the editor wars
- geany - A nice standalone GUI editor with indentation and code highlighting, but you will need to switch on X forwarding to use it (so
ssh -Y -p 24 multithr@gks-XXX.scc.kit.edu
)
We have also setup working VNC on these machines, if you would prefer to use a VNC client from your laptop.
If you want us to really recommend something then we pick geany
;-)
Exercises
Introductions to each section of the tutorial are here:
https://github.com/graeme-a-stewart/cpp-concurrency/tree/gridka14/doc
and exercises are at the end of each document.
The file
https://github.com/graeme-a-stewart/cpp-concurrency/blob/gridka14/doc/XX-Answers.md
lists the source code that is the solution to each of the exercises. Feel free to look at that if you need a hint or want to check your answer.
You can clone the git repository like this:
git clone https://github.com/graeme-a-stewart/cpp-concurrency.git -b gridka14
N.B. This will clone the GridKA 2014 version of the repository, if you want to clone the master branch then just do
git clone https://github.com/graeme-a-stewart/cpp-concurrency.git
References
https://github.com/graeme-a-stewart/cpp-concurrency/blob/master/doc/00-TipsReferences.md
Is the current 'master' copy of the introduction, which provides references.