std::promise在我的机器上被破坏了吗(使用g++-mp)

Is std::promise broken on my machine (using g++-mp)?

本文关键字:坏了 g++-mp 使用 promise 我的 机器 std      更新时间:2023-10-16

这个代码是有效的,还是我的编译器坏了?

#include <future>
#include <iostream>
int main() {
   std::cout << "doing the test" << std::endl;
   std::promise<bool> mypromise;
   std::future<bool> myfuture = mypromise.get_future();
   mypromise.set_value(true);
   bool result = myfuture.get();
   std::cout << "success, result is " << result << std::endl;
   return 0;
}

输出如下:

$ g++-mp-4.8 -std=c++11 test.cpp
$ ./a.out
doing the test
Segmentation fault: 11
$ 

我正在使用g++-mp-4.8,这是来自macports的gcc 4.8。

我疯了吗?

动态链接器可能将您的程序链接到libstdc++的旧版本,即/opt/local/lib/libstdc++.6.dylib中的那个

因为你是用GCC 4.8编译的,你需要使用GCC 4.8附带的新的libstdc++,它可能是/opt/local/lib/gcc48/libstdc++.6.dylib

您应该检查/opt/local/lib/libstdc++.6.dylib是否是GCC 4.8附带的库,如果不是,则使用正确的库。

可以通过多种方式控制,最简单的(但不一定是最好的)是运行:

export DYLD_LIBRARY_PATH=/opt/local/lib/gcc48/
./a.out

参见http://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.how_to_set_paths和http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dynamic_or_shared.html#manual.intro.using.linkage.dynamic获取其他信息(这不是特定于Mac OS X)

你的代码在Xcode中编译和运行良好。输出为

做测试成功,结果是1

编译器是Apple LLVM 4.2

因此,我建议您有编译器配置问题

这是一个MacPorts bug。MacPorts上已经有bug报告了。你可以在这里输入:

https://trac.macports.org/ticket/38814(主要问题)
https://trac.macports.org/ticket/38833(我的报告,目前标记为#38814的副本)

确认:您的代码在这里产生相同的问题:

编译器:g++ -v给出

 Using built-in specs.
 COLLECT_GCC=/opt/local/bin/g++-mp-4.8
 COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin12/4.8.1/lto-wrapper
 Target: x86_64-apple-darwin12

配置:./gcc-4.8-20130328/configure——prefix=/opt/local——build=x86_64-苹果-darwin12——enable-languages=c,c++,objc, objc -c++,fortran,java——libdir=/opt/local/lib/gcc48——includedir=/opt/local/include/gcc48——infodir=/opt/local/share/info——mandir=/opt/local/share/man——datarootdir=/opt/local/share/gcc-4.8——with-local-prefix=/opt/local——with-system-zlib——disable-nls——program-suffix=-mp-4.8——with-gxx-include-dir=/opt/local/include/gcc48/c++/——with-gmp=/opt/local——with-mpfr=/opt/local——with-mpc=/opt/local——with-ppl=/opt/local——with- clog =/opt/local——enable- clog -backend=isl——disable- clog -version-check——enable-stage1-checking——disable-multilib——enable-lto——enable-libstdcxx-time——with-as=/opt/local/bin/as——with-ld=/opt/local/bin/ld——with-bugurl=https://trac.macports.org/newticket——with-pkgversion='MacPorts gcc48 4.8-20130328_0'

 Thread model: posix
 gcc version 4.8.1 20130328 (prerelease) (MacPorts gcc48 4.8-20130328_0)

在GDB中运行得到:

doing the test
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: 13 at address: 0x0000000000000000
0x0000000100081c30 in __once_proxy ()
(gdb) up
#1  0x00007fff8872fff0 in pthread_once ()
(gdb) up
#2  0x0000000100000fd2 in __gthread_once ()
(gdb) up
#3  0x00000001000020c9 in _ZSt9call_onceIMNSt13__future_base11_State_baseEFvRSt8functionIFSt10unique_ptrINS0_12_Result_baseENS4_8_DeleterEEvEERbEIKPS1_St17reference_wrapperIS8_ESF_IbEEEvRSt9once_flagOT_DpOT0_ ()
(gdb) up
#4  0x0000000100001886 in std::__future_base::_State_base::_M_set_result ()
(gdb) up
#5  0x00000001000025ef in std::promise<bool>::set_value ()
(gdb) up
#6  0x000000010000119f in main ()

所以这似乎是libstdc++中的一些错误。顺便说一句,使用clang 3.2(自制),这将编译&运行良好(result is 1)。也许你应该用bugzilla来提交一个bug报告…

如果您不需要使用GCC,则可以使用最新版本的Apple编译器。确保你安装了最新的Xcode和Xcode中的"命令行工具"。

命令行应该是:

clang++ -std=c++11 -stdlib=libc++ -pthread test.cpp