在OS X上,Pthread和gcc编译问题

4

我有一个脚本,在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行:

以下是代码的前22行:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
#include <pthread.h>
#include <assert.h>

/* 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;

有什么想法是为什么会发生这种情况吗?

似乎未定义pthread_barrier_t。 - asaelr
据我所知,这是由pthread库定义的一种类型。 - Dolan Antenucci
3
OSX可能没有pthread_barrier_t。关于这一点的一些提示已经在这个问题中提到了。 - another.anon.coward
1
好的,这样就清楚了。Linux 赢了 :) 谢谢 - Dolan Antenucci
1
是的,OS X 似乎有些像在 2001 年左右冻结的 POSIX。帮助人们编写符合现代标准的代码似乎不是他们的首要目标。 - Jens Gustedt
2个回答

17
根据opengroup.org上关于pthread_barriers的信息,屏障被定义在POSIX标准的可选部分中;该选项的名称为"(高级实时线程)",有时更精确地称为"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方面也遇到了一些问题。


2
就像osgx提到的那样,OS X上没有实现屏障,但您可以随时实现它或者只使用这个实现。关于以前的实现的快速说明,您可以使用osgx提到的宏_POSIX_BARRIERS,而不是博客上的宏,例如#if !defined _POSIX_BARRIERS || _POSIX_BARRIERS < 0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接