我正在试验 C++0x 线程,部分在 gcc 4.5 中实现,但我遇到了一个我无法理解的问题。让我们看看这段代码
#include <future>
#include <iostream>
int main()
{
std::cout << std::async([]() { return 10; }).get() << std::endl;
}
这很简单,应该可以工作,但事实并非如此。它抛出 std::system_error
terminate called after throwing an instance of 'std::system_error' what(): Aborted
what() 返回空字符串,因此根本没有错误信息。更重要的是,它不适用于在线编译器,它也使用 gcc 4.5,但现在它可以工作了。你可以在这里看到
http://liveworkspace.org/code/f3003d62a758e047a880920c84f1241c
但是昨天它抛出了相同的异常(尽管服务器上没有进行任何更改)。你知道是什么问题吗?
谢谢。
最佳答案
尝试使用 -lpthread 链接。出于某种原因,线程库确实在没有 pthread 的情况下编译,但在运行时崩溃。
关于multithreading - C++0x 的 gcc 实验性实现的 std::future 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3930700/