2013-06-25 13 views
5

Qualcuno può spiegare quello che sto facendo male qui:C++ 11 asincrone segmentation fault

#include <future> 
#include <iostream> 


int main() {  
    std::future<int> result(std::async([](int m) { return m+1;}, 2)); 
    std::cout << "In main" << std::endl; 
    std::cout << result.get(); 
} 

con GCC 4.8 o 4.9, ottengo un errore di segmentazione. :

./a.out: 


In main 
     Segmentation fault: 11 



    : /opt/local/bin/g++-mp-4.8 -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 
    Configured with: ../gcc-4.8.1/configure --prefix=/opt/local --build=x86_64-apple-darwin12 --enable-languages=c,c++,objc,obj-c++,lto,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-cloog=/opt/local --enable-cloog-backend=isl --disable-cloog-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-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --with-pkgversion='MacPorts gcc48 4.8.1_0' 
    Thread model: posix 
    gcc version 4.8.1 (MacPorts gcc48 4.8.1_0) 

Edit: Alcune persone hanno fatto per un'altra risposta che dice legame con pthread. Questo non risolve il problema:

: /opt/local/bin/g++-mp-4.8 -std=c++11 -lpthread thread5.cc 
: ./a.out 
In main 
Segmentation fault: 11 
+5

Ti sei collegato con '-pthread'? – hmjd

+2

[Sembra funzionare su 4.8.1] (http://coliru.stacked-crooked.com/view?id=f82d46302006f30b481f84cf831fb374-08d1f1279a7cb98460a995a1b0c84fe6). – Praetorian

+0

Con tutto il rispetto, questo non è un duplicato. Ho fatto il collegamento con lpthread e si blocca ancora: $: /opt/local/bin/g++-mp-4.8 -std = C++ 11 -lpthread thread5.cc $./A.out In main Errore di segmentazione: 11 – duli

risposta

0

Ho testato il codice con clang 3.2 (apple llvm 4.2) e funziona perfettamente. restituisce

In main 
3 

edit: altri anche persone testate con gcc 4.8.1 e funziona. Il problema più probabile è che tu stia collegando la libreria pthread predefinita invece di quella fornita con gcc 4.8.1 da macports

Problemi correlati