2011-12-07 57 views
6

我剛剛在我的ubuntu 10.04上編譯並安裝了clang + llvm 3.0,還從svn中安裝了libC++。由於libC++中的狀態顯示線程支持已完成,我想嘗試std :: async。所以我按照安東尼·威廉姆斯給出的例子中std :: async 3.0中的異步+ libC++不起作用?

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; 
} 

我編譯

鐺++ --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 in std::exception_ptr::~exception_ptr (this=) at ../src/exception.cpp:130 
#3 0x0000000000404178 in void std::__1::__assoc_state::set_value(int&&)() 
#4 0x00000000004051ae in _ZNSt3__119__async_assoc_stateIiNS_12__async_funcIPFivEJEEEE9__executeEv() 
#5 0x0000000000404e00 in _ZNSt3__114__thread_proxyINS_5tupleIJMNS_19__async_assoc_stateIiNS_12__async_funcIPFivEJEEEEEFvvEPS7_EEEEEPvSC_() 
#6 0x00007fd0a250f9ca in start_thread() from /lib/libpthread.so.0 
#7 0x00007fd0a1b2e70d in clone() from /lib/libc.so.6 
#8 0x0000000000000000 in ??() 

有人有一個想法,爲什麼?

+2

中止意味着斷言失敗。查看第130行附近的exception.cpp中的assert()s。強烈建議使用gdb來中止和分析當地人。 – moshbear

回答

7

我跑了OS X Lion的例子,使用:

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

而且程序輸出:

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

檢查的libC++源通過moshbear的評論的建議我看到:

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++中實現。致力於創建此級別的免GPL實施的工作正在進行中,地址爲libc++abi。我可以向你保證,libC++ abi目前還沒有準備好黃金時段。

在這個低級圖書館也有一個獨立的努力:https://github.com/pathscale/libcxxrt。我不知道這個庫的狀態,也不知道它是否已經移植到Ubuntu。