【问题标题】:Pthread and gcc compiling issue on OS XOS X 上的 Pthread 和 gcc 编译问题
【发布时间】:2012-02-13 19:15:03
【问题描述】:

我有一个脚本可以在 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 <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。
  • 据我了解这是pthread库定义的类型
  • OSX 可能没有pthred_barrier_tthis question 中提到了一些关于此的提示
  • 嗯,这说明了这一点。 Linux 的胜利 :) 谢谢
  • 是的,OS X 似乎有点像 2001 年左右冻结的 POSIX。帮助人们编写符合现代标准的代码似乎并不是他们的首要目标。

标签: c macos gcc compiler-errors pthreads


【解决方案1】:

根据 opengroup.org 上的info about pthread_barriers,障碍在 POSIX 标准的 optional 部分中定义;选项的名称是“(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 兼容,但无法在线获得已实施选项的完整列表。 There is a letter 在 2006 年的苹果 mainling 列表中,表示 Mac OS X 没有障碍。

我知道 Solaris 在 pthread_barrier 方面也存在一些问题。

【讨论】:

    【解决方案2】:

    就像提到的 osgx 一样,屏障没有在 OS X 上实现,但你可以随时实现它或者只使用this 实现。快速说明之前的实现,您可以使用 osgx 提到的宏 _POSIX_BARRIERS,而不是博客上的宏,例如 #if !defined _POSIX_BARRIERS || _POSIX_BARRIERS &lt; 0

    【讨论】:

      猜你喜欢
      • 2011-04-12
      • 1970-01-01
      • 2012-10-25
      • 2011-11-11
      • 2013-05-28
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多