6

我刚刚在我的 ubuntu 10.04 上编译并安装了 clang+llvm 3.0,以及来自 svn 的 libc++。由于 libc++ 中的状态显示线程支持已完成,我想尝试 std::async。所以我按照安东尼威廉姆斯给出的例子

http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-8-futures-and-promises.html

只需稍作改动即可编译:

#include <future>
#include <iostream>

int calculate_the_answer_to_LtUaE()
{
  return 42;
}

void do_stuff()
{
  std::cout << "doing stuff" << std::endl;
}

int main()
{
  std::future<int> the_answer=std::async(calculate_the_answer_to_LtUaE);
  do_stuff();
  std::cout<<"The answer to life, the universe and everything is "
    <<the_answer.get()<<std::endl;
}

我编译

clang++ --std=c++0x -stdlib=libc++ -lpthread async.cpp

但是,它运行并始终以核心转储结束:

做事 生命、宇宙和一切的答案被中止(核心倾倒)

我检查了核心转储,它显示了这样的堆栈(我不太明白)

#0 0x00007fd0a1a7ba75 in raise () from /lib/libc.so.6
#1 0x00007fd0a1a7f5c0 in abort () from /lib/libc.so.6
#2 0x00007fd0a22a735b 在 std::exception_ptr::~exception_ptr (this=) at ../src/exception.cpp:130
#3 0x0000000000404178 in void std::__1::__assoc_state::set_value(int&&) ()
#4 0x00000000004051ae 在 _ZNSt3__119__async_assoc_stateIiNS_12__async_funcIPFivEJEEEE9__executeEv ()
#5 0x0000000000404e00 in _ZNSt3__114__thread_proxyINS_5tupleIJMNS_19__async_assoc_stateIiNS_12__async_funcIPFivEJEEEEEFvvvEPS7_EEEEEPvSC_ ()
#6 0x00007fd0a250f9ca in start_thread () from /lib/libpthread.so.0
#7 0x00007fd0a1b2e70d in clone () from /lib/libc.so.6
#8 0x0000000000000000 在 ?? ()

有人知道为什么吗?

4

1 回答 1

8

我在 OS X Lion 上运行了您的示例,使用:

clang++ -std=c++0x -stdlib=libc++ async.cpp

和程序输出:

doing stuff
The answer to life, the universe and everything is 42

按照 moshbear 的评论建议检查 libc++ 的来源,我看到:

exception_ptr::~exception_ptr() _NOEXCEPT
{
#if HAVE_DEPENDENT_EH_ABI
    __cxa_decrement_exception_refcount(__ptr_);
#else
    #warning exception_ptr not yet implemented
    ::abort();
#endif  // __APPLE__
}

在我看来,~exception_ptr()尚未移植到 ubuntu 10.04。这是一个在可移植 C++ 中无法实现的低级工具。libc++abi正在进行创建此级别的无 GPL 实现的工作。我可以向你保证,libc++abi 目前还没有准备好迎接黄金时段。

这个低级库也有独立的工作: https ://github.com/pathscale/libcxxrt 。我不知道这个库的状态,也不知道它是否已经移植到 ubuntu。

于 2011-12-07T00:37:39.477 回答