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?
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.
- 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.
MSVC creates a thread pool the first time you run std::async , which may become an overhead in certain situations.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With