Getting started with Eclipse CDT, Threading Building Blocks, parallel_for and C++11 lambdas

When doing concurrency with C++ you have many choices incl:

  • Posix threads: Very low level, rather ugly C API, avoid if possible.
  • C++11 concurrency features: Very interesting and good stuff, no full compiler support yet but some things already usable with some compilers.
  • Boost: Has many good things, close to the features in C++11. Ready for production.
  • Intel’s Threading Building Blocks (TBB): Many great features, definitely worth checking out for C++ concurrency work.

There are more options (do a web search). For me at the moment Intel’s Threading Building Blocks (TBB) look the most interesting (but I am following the compiler support for C++11’s concurrency support closely).

In this short tutorial I will explain how to get started with Eclipse CDT (an Eclipse plugin for C/C++), Threading Building Blocks, a parallel for loop and how to add C++11’s lambdas to the code.

I use the latest GCC (4.7.0 at the time of this writing) for all the examples. Although GCC does not yet support all of C++11 (particularly not all concurrency stuff) it already supports quite a lot of the latest features incl. lambdas which is why we can add them to our TBB example.

I assume you have GCC 4.7.0 or newer installed on your Linux machine (get it from the website and follow the installation instructions).
I’ve installed it to
/opt/cpp
You can install it where you like, adjust your settings accordingly. I’ve also set my PATH variable that the latest GCC is used and not the one that was already present on my Ubuntu 11.04.

1) Get and install TBB
I downloaded the latest TBB from the project website. I use version 4.0 here.
I downloaded both the binary distribution and the source code. Unpack both in the same directory and call
$ make
Either leave everything where it is or copy it somewhere where you want to have the libraries and header files. I’ve copied everything to:
/opt/cpp
There is now a directory:
/opt/cpp/tbb40_20120201oss
(if you have a newer version of TBB, this may look slightly more different).

2) Get and install Eclipse + CDT
I just downloaded the Eclipse IDE for C/C++ Linux Developers from the Eclipse Website and unpacked it into
/opt/eclipse/cpp
Note: I have several different Eclipse installations running. One for Java, one for Scala and more for other languages like Clojure or Groovy. I had trouble with all plugins for all the languages in one installation. Having separate installations – and separate workspaces – make things easier.

3) Create a new C++ project with C++11 support and add TBB
I just created a new Eclipse project via File -> New -> Project and then choosing C++ Project and them Empty Project with Linux GCC.
Once done I configured the project. I right-clicked on the project and selected “Properties” (down at the bottom of the menu).
The I choose C/C++ Build -> Settings. This dialog then can be a bit confusing to beginners but it is not too difficult to figure out what to do.

I opened the GCC C++ Compiler and set the following things:

Includes:
Here I added the path to the TBB include files:
/opt/cpp/tbb40_20120201oss/include/
(adjust the path according to your installation).

Miscellaneous:
Here I added this option: -std=c++11
This tells the GCC 4.7 compiler to support the latest C++11 standard. The support is not yet complete but lot’s of stuff including the lambdas used in the example code below are already supported.

For the GCC C++ Linker I set the following things:

Libraries:
At the top window, you can specify the libraries. I just added “tbb” here. Note that no “-l” was required, this is added automatically.
I also set the Library Search Path to:
/opt/cpp/tbb40_20120201oss/build/linux_intel64_gcc_cc4.7.0_libc2.13_kernel2.6.38_release/
Again, you have to adjust this for your installation. This directory is where the TBB libraries are after compiling the sources.

5) Add code and run it
I added a new C++ source file by right-clicking on the project and then chose New -> Source File (or New -> Other -> C/C++ -> Source File when Eclipse does not already show the option for source file).

I added some source code like the sample below and compiled it with “Ctrl-B” and then ran it with “Ctrl-F11”.

The interesting part is the update_parallel function which takes an array, uses a parallel_for loop and then using a lambda as an argument to parallel_foe which tells the loop what to do with each element.
This is just a very simple example and both the parallel_fore and C++11 lambdas are much more powerful than what is shown in this example.

One important note: This example is just for demonstrating how to use Eclipse CDT, C++11, lambdas and TBB. It makes absolutely no sense updating an array with 5 elements in parallel as the overhead for making this run in parallel would be much worse than just iterating over the short array. With much larger arrays and maybe much more complicated operations on each element, things change. Make sure to test the performance on your production hardware!

As you can see, once you got everything working, it is easy to use. You can also use a different compiler, e.g. Intel’s C++ compiler (which is free for non-commercial use on Linux). See Intel’s website for more information.

TBB is a very interesting library and definitely worth having a closer look when you want to do concurrency today with C++. The new C++11 standard will not support all the things that are already in TBB (e.g. the standard currently has not thread-safe collections).
TBB’s documentation is good and easy to read.

If you need concurrency and parallelism in your programs – and if you want to or must use C++ – TBB is probably your best choice right now.
Make sure to also look at other options like C++11, Boost, Cilk Plus (also from Intel), Java (which has great concurrency support and is really fast) or Scala incl. the Akka toolkit.

2 Comment

  1. Do you have any suggestions on a compiler for Windows? I am looking for one that supports the latest C++11 standard.

    1. Markus Jais says: Reply

      Best would probably be Microsoft’s compiler. They haven’t implement all of C++11 yet (no compiler has as far as I know) but this is definitely a good compiler to use.
      Intel also has a C++ compiler which supports some of C++11.

Leave a Reply

This blog is kept spam free by WP-SpamFree.