Ich habe das folgende Stück Code:Warum werden meine verketteten Methoden mit boost :: future nicht aufgerufen?
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <iostream>
#include <thread>
#include <boost/thread/future.hpp>
using namespace boost;
int foo(boost::future<int> x) {
std::cout << "first stage(" << x.get() << ")" << '\n';
return x.get();
}
int main()
{
std::cout << "making promise" << '\n';
boost::promise<int> p;
boost::future<int> f = p.get_future();
std::cout << "future chain made" << '\n';
std::thread t([&](){
f.then(foo)
.then([](boost::future<int> x){ std::cout << "second stage " << 2 * x.get() << '\n'; return 2 * x.get(); })
.then([](boost::future<int> x){ std::cout << "final stage " << 10 * x.get() << '\n'; });
});
std::cout << "fulfilling promise" << '\n';
p.set_value(42);
std::cout << "promise fulfilled" << '\n';
t.join();
}
und ich kompilieren es etwa so:
g++ -g -Wall -std=c++14 -DBOOST_THREAD_VERSION=4 main.cpp -lboost_thread -lboost_system -pthread
und ich bekomme die folgende Ausgabe:
making promise
future chain made
fulfilling promise
promise fulfilled
first stage(42)
Warum meine 2 lambdas gekettet im Thread t
nicht aufgerufen? Fehle ich etwas?
Ich habe versucht, boost::future::get()
Gespräch hinzugefügt wird, aber ich erhalte eine Ausnahme dann:
std::cout << "fulfilling promise" << '\n';
p.set_value(42);
std::cout << "promise fulfilled" << '\n';
std::cout << "value " << f.get() << '\n';
t.join();
Fehler:
making promise
future chain made
fulfilling promise
promise fulfilled
first stage(42)
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::future_uninitialized> >'
what(): Operation not permitted on an object without an associated state.
[1] 20875 abort ./main
I boost bin mit 1.58.0 und gcc 5.4.0
Online Quelllink (mit booost 1.58.0 und gcc 5.3.0) http://melpon.org/wandbox/permlink/G8rqt2eHUwI4nzz8
Mit 'std :: future' ist es illegal,' get() 'mehr als einmal aufzurufen; gilt das auch für 'boost :: future'? (Die Behauptung, die ich beim Ausführen bekomme, legt dies nahe, aber die Dokumentation macht es mir nicht klar ...) – ildjarn
@Yakk Du hast Recht, aber wir beide haben einen sehr wichtigen Punkt in meinem Code verpasst: Ich rief an x.get() 'in Callbacks zweimal, das ist UB. – Patryk
@pat faltete das ein. Wenn sie 'std' waren, machte die implizite Umwandlung in die gemeinsame Zukunft von rvalue future die Fixierung einfach: Angenommen, Boost ist so intelligent. – Yakk