Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 341
Verdagon Avatar asked Apr 03 '13 02:04

Verdagon


People also ask

What is STD promise?

The class template std::promise provides a facility to store a value or an exception that is later acquired asynchronously via a std::future object created by the std::promise object. Note that the std::promise object is meant to be used only once.

What is promise and future in C++?

- The promise object is the asynchronous provider and is expected to set a value for the shared state at some point. - The future object is an asynchronous return object that can retrieve the value of the shared state, waiting for it to be ready, if necessary.

Is STD async thread pool?

MSVC creates a thread pool the first time you run std::async , which may become an overhead in certain situations.

How does STD future work?

The class template std::future provides a mechanism to access the result of asynchronous operations: An asynchronous operation (created via std::async, std::packaged_task, or std::promise) can provide a std::future object to the creator of that asynchronous operation.


2 Answers

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)

like image 121
Jonathan Wakely Avatar answered Sep 29 '22 13:09

Jonathan Wakely


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

like image 39
Peter Cogan Avatar answered Sep 29 '22 13:09

Peter Cogan