Using Intel TBB in C

I am trying to use Intel TBB in C. All the documentation I get for TBB is C ++ oriented. Does TBB work with plain C? If so, how to determine the atomic integer. In the code below, I tried using a pattern (I know this won't work in C). Is there any way to fix this? atomic<int> counter

#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
+3
source share
3 answers

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

Try the following:

#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);
}

.cpp( g++). tbb:: atomic, , main. . -std = ++ 11 nullptr NULL.

+3

C ++ - . Intel TBB - ++.

+1

Do not do that. This will be a disaster, even if the code is compiled, because the TBB kernel itself has an automatic task scheduler, which itself is heavily disguised. C does not support general programming, and TBB is not intended for use with C. If you see the documentation, usage guidelines, and comparisons on the TBB page, they gladly recommend that you use other parallelization constructs than TBB. If you are stuck with using C, go for openMP. OpenMP 3 has a parallelism task. However, the strength of the sophisticated algorithms available on TBB is still lacking.

+1
source

All Articles