【问题标题】:using Intel TBB in C在 C 中使用英特尔 TBB
【发布时间】:2014-02-13 09:29:32
【问题描述】:

我正在尝试在 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

【问题讨论】:

  • 英特尔 TBB 是“用于 的 C++ 模板库”。是什么让您认为可以在 C 中使用它?
  • 来自official pageIntel® Threading Building Blocks (Intel® TBB) lets you easily write parallel C++ programs
  • 谢谢你们。我想尽可能地坚持使用普通的旧 C。现在我想这是不可能的。顺便说一句,否决票真的有必要吗?
  • 虽然从纯 C 中使用 TBB 确实是不可能的,但我绝对不明白为什么现在几乎所有关于混合 C 和 C++ 的 SO 问题都被认为是不好的。 IMO 您的问题是适当的,不应受到惩罚。
  • 尝试 C11 atomics、GCC 内部函数或 Argonne OpenPA。

标签: c++ c intel tbb


【解决方案1】:

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 扩展名并使用了 g++。编译并运行良好。
【解决方案2】:

C 和 C++ 是不同的语言。英特尔 TBB 只是一个 C++ 库。

【讨论】:

    【解决方案3】:

    不要那样做。即使代码可以编译,也将是灾难性的,因为在 TBB 的核心,有一个自动任务调度程序,它本身是大量模板化的。 C 不支持泛型编程,而且 TBB 不适合与 C 一起使用。 如果您在 TBB 的页面上看到文档、使用建议和比较,他们会很高兴地建议您使用其他并行化结构而不是 TBB。 如果您坚持使用 C,请选择 openMP。 OpenMP 3 具有基于任务的并行性。但是,它仍然缺乏 TBB 上可用的复杂算法的强度。

    【讨论】:

      猜你喜欢
      • 2011-10-31
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多