OS X上的Pthread和gcc编译问题

我有一个在Linux上编译好的脚本(Ubuntu 11.04),但在OS X(Lion)上没有。

gcc -pthread -o hw1 hw1.c hw1.c:22: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'barr' hw1.c: In function '__syncthreads': hw1.c:53: error: 'barr' undeclared (first use in this function) hw1.c:53: error: (Each undeclared identifier is reported only once hw1.c:53: error: for each function it appears in.) hw1.c:54: error: 'PTHREAD_BARRIER_SERIAL_THREAD' undeclared (first use in this function) hw1.c: In function 'parallel_psum': hw1.c:94: error: 'barr' undeclared (first use in this function) hw1.c:107: warning: assignment from incompatible pointer type 

这是代码的前22行:

 #include  #include  #include  #include  #include  #include  #include  /* create thread argument struct for thr_func() */ typedef struct _thread_data_t { int tid; int* ints; int* sums; int num_ints; int* temp; } thread_data_t; const int MIN_RAND_INT = 1; const int MAX_RAND_INT = 65000; // pthreads barrier variable pthread_barrier_t barr; 

任何想法为什么会这样?

根据有关opengroup.org上pthread_barriers的信息 ,在POSIX标准的可选部分中定义了障碍; 选项的名称是“ (ADVANCED REALTIME THREADS) ”,有时更准确地称为“BAR,障碍(实时)”。

http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap02.html

系统可能支持以下符号常量表示的一个或多个选项(请参阅选项):

 _POSIX_BARRIERS 

因此,只有将_POSIX_BARRIERS宏定义为正数时,才能使用pthread_barrier_t或pthread_barrier_wait。

Mac OS X符合POSIX标准,但在线无法提供已实施选项的完整列表。 2006年的苹果主要名单中有一封信 ,其中表示Mac OS X中没有障碍。

我知道Solaris也有一些pthread_barrier问题。

就像osgx提到的那样,OS X上没有实现障碍,但你可以随时实现它或只使用这个实现。 关于上一个实现的快速说明,你可以使用osgx提到的宏_POSIX_BARRIERS,而不是博客上的那些,像这样#if !_POSIX_BARRIERS