【发布时间】: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 page:
Intel® Threading Building Blocks (Intel® TBB) lets you easily write parallel C++ programs -
谢谢你们。我想尽可能地坚持使用普通的旧 C。现在我想这是不可能的。顺便说一句,否决票真的有必要吗?
-
虽然从纯 C 中使用 TBB 确实是不可能的,但我绝对不明白为什么现在几乎所有关于混合 C 和 C++ 的 SO 问题都被认为是不好的。 IMO 您的问题是适当的,不应受到惩罚。
-
尝试 C11 atomics、GCC 内部函数或 Argonne OpenPA。