Verdagon
Verdagon

Reputation: 2610

Is std::promise broken on my machine (using g++-mp)?

Is this code valid, or is my compiler broken?

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

Here's the output:

$ g++-mp-4.8 -std=c++11 test.cpp
$ ./a.out
doing the test
Segmentation fault: 11
$ 

I'm using g++-mp-4.8, which is the gcc 4.8 from macports.

Am I going insane?

Upvotes: 17

Views: 1799

Answers (5)

Yongwei Wu
Yongwei Wu

Reputation: 5582

If you do not have to use GCC, the latest version of the Apple compiler works. Be sure you install the latest Xcode and also the "command line tools" from within Xcode.

The command line should be:

clang++ -std=c++11 -stdlib=libc++ -pthread test.cpp

Upvotes: 0

Yongwei Wu
Yongwei Wu

Reputation: 5582

This is a MacPorts bug. There are already bug reports on MacPorts. You can follow there:

https://trac.macports.org/ticket/38814 (main issue)
https://trac.macports.org/ticket/38833 (my report, currently marked as duplicate of #38814)

Upvotes: 1

Walter
Walter

Reputation: 45414

confirmed: your code produces the same problem here:

compiler: g++ -v gives

 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-20130328/configure --prefix=/opt/local --build=x86_64-apple-darwin12 --enable-languages=c,c++,objc,obj-c++,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-20130328_0'

 Thread model: posix
 gcc version 4.8.1 20130328 (prerelease) (MacPorts gcc48 4.8-20130328_0)

running it in gdb gives:

doing the test

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: 13 at address: 0x0000000000000000
0x0000000100081c30 in __once_proxy ()
(gdb) up
#1  0x00007fff8872fff0 in pthread_once ()
(gdb) up
#2  0x0000000100000fd2 in __gthread_once ()
(gdb) up
#3  0x00000001000020c9 in _ZSt9call_onceIMNSt13__future_base11_State_baseEFvRSt8functionIFSt10unique_ptrINS0_12_Result_baseENS4_8_DeleterEEvEERbEIKPS1_St17reference_wrapperIS8_ESF_IbEEEvRSt9once_flagOT_DpOT0_ ()
(gdb) up
#4  0x0000000100001886 in std::__future_base::_State_base::_M_set_result ()
(gdb) up
#5  0x00000001000025ef in std::promise<bool>::set_value ()
(gdb) up
#6  0x000000010000119f in main ()

so this seems some bug in libstdc++. Btw, using clang 3.2 (homebrew), this compiles & runs fine (result is 1). Perhaps you should file a bug report with bugzilla ...

Upvotes: 0

Peter Cogan
Peter Cogan

Reputation: 865

Your code compiles and runs fine for me in Xcode. The output is

doing the test success, result is 1

The compiler is Apple LLVM 4.2

Thus, I suggest you have a compiler configuration problem

Upvotes: 1

Jonathan Wakely
Jonathan Wakely

Reputation: 171263

The dynamic linker may be linking your program to an old version of libstdc++, the one in /opt/local/lib/libstdc++.6.dylib

Since you're compiling with GCC 4.8 you need to use the new libstdc++ that comes with GCC 4.8, which is probably /opt/local/lib/gcc48/libstdc++.6.dylib

You should check whether /opt/local/lib/libstdc++.6.dylib is the library that comes with GCC 4.8 and use the right one if it isn't.

You can control that in various ways, the simplest (but not necessarily the best) would be to run:

export DYLD_LIBRARY_PATH=/opt/local/lib/gcc48/
./a.out

See http://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.how_to_set_paths and http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dynamic_or_shared.html#manual.intro.using.linkage.dynamic for other info (which is not specific to Mac OS X)

Upvotes: 1

Related Questions