【发布时间】:2013-04-03 02:53: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。
我要疯了吗?
【问题讨论】:
-
好吧,它在 Ideone 中也崩溃了:ideone.com/wNsr1h。我不太了解这些库,但我猜这可能不是使用它们的正确方法。
-
在 Linux/g++-4.7.2 上没有段错误,但会抛出
std::system_error。 -
在本地 GCC 4.8 上,我只能在添加
-pthread命令行选项时才能使其工作(在执行与std::thread相关的任何操作时通常需要此选项,可能包括 @987654327 @ 和std::promise也是如此)。 -
@Verdagon 我不确定在这种情况下是否有所不同,但该选项实际上应该是
-pthread,而不仅仅是链接器选项-lpthread。 (在我的 GCC 上,它不是g++-mp,但它也适用于-lpthread。) -
尝试运行
otool -L ./a.out以确保您使用的是 GCC 4.8 中的libstdc++.dylib而不是更旧的系统库