Concurrent Programming in C++ (2016)

From Gridkaschool
Jump to: navigation, search

Contents

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++ template library 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

The machines we'll use in the tutorial are CentOS 7 machines. On these the native C++ compiler is gcc4.8.3, which has C++11 support and supports all the tutorial exercises.

As as aside, if you want the very latest C++14 standard support, gcc6 is recommended, but for this tutorial C++11 contains all we need.

If you wish to use your own machine to do the tutorial exercises then please make sure your gcc version is 4.8 at minimum!

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
  • -lpthread - 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. (You can make customisations for your system by defining CXX and/or CXXFLAGS as environment variables.)

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 v4.4 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. (In fact 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.

If you have a machine or distribution on which TBB is installed, and you intend to use that, please make sure it's an up to date version, at least v4.2.

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 gks@concurrent-N)

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/gridka16/doc

and exercises are at the end of each document.

The file

https://github.com/graeme-a-stewart/cpp-concurrency/blob/gridka16/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 gridka16

N.B. This will clone the GridKA 2016 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

(note that the master branch uses an updated CMake setup that will not work out of the box on CentOS7).

References

https://github.com/graeme-a-stewart/cpp-concurrency/blob/gridka16/doc/00-TipsReferences.md

provides references.

Contact

Graeme Stewart email

Personal tools