Question

I'm trying to use Intel TBB in C. All the documentation I get for TBB are targeted towards C++. Does TBB work with plain C? If yes how do I define an atomic integer. In the below code I tried using a template atomic<int> counter (I know this will not work in C). Is there a way to fix this?

#include<pthread.h>
#include<stdio.h>
#include<tbb/atomic.h>

#define NUM_OF_THREADS 16

atomic<int> counter;

void *simpleCounter(void *threadId)
{
    int i;
    for(i=0;i<256;i++)
    {
        counter.fetch_and_add(1);
    }
    printf("T%ld \t Counter %d\n", (long) threadId, counter);
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    counter=0;
    pthread_t threadArray[NUM_OF_THREADS];
    long i;
    for(i=0;i<NUM_OF_THREADS;i++)
    {
        pthread_create(&threadArray[i], NULL, simpleCounter, (void *)i);
    }
    pthread_exit(NULL);
}


-bash-4.1$  g++ simpleCounter.c -I$TBB_INCLUDE -Wl,-rpath,$TBB_LIBRARY_RELEASE -L$TBB_LIBRARY_RELEASE -ltbb
simpleCounter.c:7: error: expected constructor, destructor, or type conversion before ‘<’ token
simpleCounter.c: In function ‘void* simpleCounter(void*)’:
simpleCounter.c:16: error: ‘counter’ was not declared in this scope
simpleCounter.c:18: error: ‘counter’ was not declared in this scope
simpleCounter.c: In function ‘int main(int, char**)’:
simpleCounter.c:24: error: ‘counter’ was not declared in this scope
Was it helpful?

Solution

hivert is right, C and C++ are different languages.

Try this:

#include<pthread.h>
#include<stdio.h>
#include<tbb/atomic.h>

#define NUM_OF_THREADS 16

tbb::atomic<int> counter;

void *simpleCounter(void *threadId)
{
    int i;
    for(i=0;i<256;i++)
    {
        counter.fetch_and_add(1);
    }
    printf("T%ld \t Counter %d\n", (long) threadId, (int)counter);
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    counter=0;
    pthread_t threadArray[NUM_OF_THREADS];
    long i;
    for(i=0;i<NUM_OF_THREADS;i++)
    {
        pthread_create(&threadArray[i], NULL, simpleCounter, (void *)i);
    }
    for(i=0;i<NUM_OF_THREADS;i++)
        pthread_join(threadArray[i], nullptr);
}

Save it with .cpp extension (not required with g++). I modified the missing namespace tbb::atomic and I also included a join in the end, to wait all threads to finish before quitting main. It should compile now. Add -std=c++11 as a compiler option, or change nullptr to NULL.

OTHER TIPS

C and C++ are different language. Intel TBB is only a C++ library.

Don't do that. It will simply be disastrous even if the code compiles because at the very core of TBB, there's an automatic task scheduler which itself is heavily templated. C doesn't support generic programming and TBB is not meant to be used with C. If you see the documentation, usage suggestions and comparisons on TBB's page, they would happily recommend you to use other parallelising constructs rather than TBB. If you are stuck with using C, go for openMP. OpenMP 3 has task based parallelism. However it still lacks the strength of complex algorithms that's available on TBB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top