07 November 2015

Using TBB's concurrent containers

When searching for a tutorial on TBB's concurrent containers, you might end up finding a 1 minute video on it which doesn't play because you don't have Flash installed, or might just want a tiny program to show how to start using it. This blog post shows you exactly that!

You can use a TBB concurrent vector exactly like you use an std::vector.

#include <iostream>
#include <tbb/concurrent_vector.h>

int main(int argc, char** argv)
    tbb::concurrent_vector vec;
    tbb::concurrent_vector::iterator vecIter;
    for(vecIter = vec.begin(); vecIter != vec.end(); vecIter++)
       std::cout<< *vecIter << "\n";
    return 0;

Just make sure you

  • Include TBB's includes folder (/usr/local/tbb44_20150728oss/include/)
  • Point the project to TBB's lib folder (/usr/local/tbb44_20150728oss/lib/ia32/gcc4.4/libtbb.so)
  • Add TBB's lib folder to LD_LIBRARY_PATH in ~/.bashrc. (export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/tbb44_20150728oss/lib/ia32/gcc4.4/")

For concurrent queue, the pop function is different.

#include <iostream>
#include <tbb/concurrent_queue.h>

int main()
    tbb::concurrent_queue q;
    q.try_pop();//This is thread safe. Better than using if (!q.empty()) {q.pop();}

No comments: