OS X 上的 Pthread 和 gcc 编译问题

发布于 2024-12-27 16:31:52 字数 1210 浏览 1 评论 0原文

我有一个脚本可以在 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;

你知道为什么会发生这种情况吗?

I have a script that compiles fine on Linux (Ubuntu 11.04), but not on 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

Here's the first 22 lines of the code:

#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;

Any ideas why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2025-01-03 16:31:52

根据 opengroup.org 上的有关 pthread_barriers 的信息,障碍是在可选< /em> 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 年的苹果 mainling 列表中有一封信,上面写着没有Mac OS X 中的屏障。

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

According to info about pthread_barriers on opengroup.org, barriers are defined in the optional part of POSIX standard; the name of option is "(ADVANCED REALTIME THREADS)", sometimes more exact referred as "BAR, barriers (real-time)".

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

The system may support one or more options (see Options) denoted by the following symbolic constants:

_POSIX_BARRIERS

So, only if the _POSIX_BARRIERS macro is defined as positive number, you can use pthread_barrier_t or pthread_barrier_wait.

Mac OS X is POSIX Compliant, but full list of implemented options is not available online. There is a letter in apple mainling list from 2006, which says there is no barriers in Mac OS X.

I know that Solaris had some problems with pthread_barrier too.

雄赳赳气昂昂 2025-01-03 16:31:52

就像 osgx 提到的那样,屏障在 OS X 上没有实现,但您始终可以实现它或仅使用 这个实现。关于之前的实现,您可以使用 osgx 提到的宏 _POSIX_BARRIERS,而不是博客上的宏,例如 #if !define _POSIX_BARRIERS || _POSIX_BARRIERS < 0

Just like osgx mentioned, barriers are not implemented on OS X, but you can always implement it or just use this implementation. Quick note on the previous implementation, you can use the macro that osgx mentioned, _POSIX_BARRIERS, instead of the ones on the blog, like this #if !defined _POSIX_BARRIERS || _POSIX_BARRIERS < 0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文