MacPorts gcc4.5 中的 std::thread [英] std::thread in MacPorts gcc4.5

查看:41
本文介绍了MacPorts gcc4.5 中的 std::thread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译一些我在 Linux 中编写的软件,这些软件在我的 Mac 上使用了一些新奇的 C++0x 功能.我使用 MacPorts 安装 gcc45 包,它给了我/opt/local/bin/g++-mp-4.5,但是这个编译器不想编译 <thread> 中的任何东西.例如我尝试编译:

I'm trying to compile some software I've been writing in Linux that uses some fancy new C++0x features on my Mac. I used MacPorts to install the gcc45 package, which gave me /opt/local/bin/g++-mp-4.5, however this compiler doesn't want to compile anything in <thread>. Eg I try to compile:

//test.cpp
#include <thread>

int main()
{
std::thread x;
return 0;
}

然后得到:

bash-3.2$ /opt/local/bin/g++-mp-4.5 -std=c++0x test.cpp 
test.cpp: In function 'int main()':
test.cpp:5:2: error: 'thread' is not a member of 'std'
test.cpp:5:14: error: expected ';' before 'x'

快速浏览/opt/local/include/gcc45/c++/thread 会发现 std::thread 类已定义,但受以下内容保护:

A quick look in /opt/local/include/gcc45/c++/thread shows that the std::thread class is defined, but is guarded by the following:

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)

再次,这在我的 Ubuntu 机器上完美运行,那么在 MacPorts 版本的 g++ 4.5 (g++-mp-4.5) 下启用 c++0x <thread> 库的正确方法是什么)?如果做不到这一点,在我自己编译 gcc 4.5 之前,我需要知道什么(配置标志等)吗?

Again, this works perfectly on my Ubuntu machine, so what's the proper way to enable the c++0x <thread> library under the MacPorts version of g++ 4.5 (g++-mp-4.5)? Failing that, is there anything I need to know (configure flags, etc.) before I go about compiling gcc 4.5 myself?

更新:看起来 SO 社区对此知之甚少,所以也许是时候与开发人员走得更近了.有谁知道我可以将这个问题转发到的官方邮件列表?有什么礼仪提示可以帮助我得到答案吗?

Update: It doesn't look like the SO community knows much about this, so maybe it's time to go a little closer to the developers. Does anyone know of an official mailing list I could forward this question to? Are there any etiquette tips to help me get an answer?

更新 2:我问了另一个临时解决方案这里,所以我现在只是用 boost::thread 库代替 std 库.不幸的是,没有 boost::future 所以这还不是一个完整的解决方案.任何帮助仍将不胜感激.

Update 2: I asked SO for another temporary solution here, and so I'm now just substituting the boost::thread libraries for the std ones. Unfortunately, there is no boost::future so this isn't quite a full solution yet. Any help would still be greatly appreciated.

推荐答案

其实 <thread> 库在 Mac OS X 下是不行的,因为这里的 pthreads 没有一些带超时的函数(例如 pthread_mutex_timedlock()).必须使用 检查 使用 _POSIX_TIMEOUTS 宏,但它在 Mac OS X 10.4、10.5 和 10.6 中被定义为 -1(我不知道 10.7 是什么),并且在 pthread 中确实没有此功能.h.

Actually <thread> library doesn't work under Mac OS X because pthreads here don't have some functions with timeouts (e.g. pthread_mutex_timedlock()). Availability of this functions have to be checked using _POSIX_TIMEOUTS macro but it's defined to -1 in Mac OS X 10.4, 10.5 and 10.6 (I don't know what's about 10.7) and this functions are really absent in pthread.h.

_POSIX_TIMEOUTS 宏在配置 libstdc++ 期间被检查.如果检查成功结束,_GLIBCXX_HAS_GTHREADS 宏被定义.<thread> 内容可通过 -std=c++0x 获得.

The _POSIX_TIMEOUTS macro is checked during the configuration of libstdc++. If the check ends successfully _GLIBCXX_HAS_GTHREADS macro becomes defined. And <thread> contents become available with -std=c++0x.

libstdc++ 确实需要 _POSIX_TIMEOUTS 例如在 std::timed_mutex 类实现中(参见 <mutex> 标头).

libstdc++ really needs _POSIX_TIMEOUTS e.g. in std::timed_mutex class implementation (see <mutex> header).

总而言之,我认为当 GCC 的 gthreads 或 libstdc++ 将实现 pthread_mutex_timedlock() (和其他)仿真或者当这个函数将在Mac OS X.

To summarize, I think that <thread> would become available on Mac OS X when GCC's gthreads or libstdc++ will implement pthread_mutex_timedlock() (and others) emulation or when this functions will be implemented in Mac OS X.

或者在未来的 C++ 标准中可能会有一种方法来查询语言特性(例如这个定时函数和类),并且可以在禁用这个特性的情况下构建 libstdc++.但是我对未来的标准不是很熟悉,并且对该功能有疑问.

Or maybe there would be a way in the future C++ standard to query for language features (e.g. this timed functions and classes) and it will be possible to build libstdc++ with this features disabled. However I'm not very familiar with the future standard and have doubts about that feature.

这篇关于MacPorts gcc4.5 中的 std::thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆