ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 在C中使用英特尔TBB

2019-08-31 04:08:10  阅读:214  来源: 互联网

标签:c-3 c intel tbb


我正在尝试在C中使用英特尔TBB.我为TBB获得的所有文档都针对C语言. TBB是否与普通C一起使用?如果是,我如何定义原子整数.
在下面的代码中,我尝试使用模板atomic< int> counter(我知道这在C中不起作用).有没有办法来解决这个问题?

#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

解决方法:

hivert是对的,C和C是不同的语言.

试试这个:

#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 = c 11作为编译器选项,或将nullptr更改为NULL.

标签:c-3,c,intel,tbb
来源: https://codeday.me/bug/20190831/1772786.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有