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
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:
There is now a directory:
(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
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:

Here I added the path to the TBB include files:
(adjust the path according to your installation).

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:

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:
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”.

#include <iostream>
#include "tbb/parallel_for.h"

using namespace tbb;
using namespace std;

void mult_by_two(int &i) {
	i = i * 2;

void update_parallel(int* a, int n) {
	parallel_for(0, n, 1, [=](int i) {

void print_array(int* a, int n) {
	for(int i = 0; i < n; i++) {
		cout << a[i] << ":";
	cout << endl;

int main() {
	/* NOTE: This is just for showing how to use the parallel_for loop
	 In practice it makes absolutely no sense to parallelize this with
	 an array that has only 5 elements! */
	int l = 5;
	int numbers[l];
	for(int i = 0; i < l; i++) {
		numbers[i] = i;
	print_array(numbers, l);
	update_parallel(numbers, l);
	print_array(numbers, l);

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.