[thread] Is there a non-blocking future-destructor?
Hi, I have stumbled on a minor inconvenience when it comes to using boost::future. Below is a small example which first wraps an asio io_service as an Executor (see even further below) and then uses boost::async() to launch a simple task on the executor and tries to queue up a continuation using future::then(Ex, F): 1. the destructor of async_result does not block which is nice 2. the destructor of continuation_result does block which is a bit of an annoyance as this future I do not care about at all Is there a way to make the destructor not block? /M int main(int argc, char* argv[]) { boost::asio::io_service io_service; asio_executor executor{ io_service }; auto async_result = boost::async(executor, []() { return 42; }); // hangs: // async_result.then(executor, &g); // does not hang: auto continuation_result = async_result.then(executor, [](boost::future<int> r) { cout << r.get() << endl; }); cout << "start io_service" << endl; io_service.run(); cout << "stop io_service" << endl; return 0; } class asio_executor : public boost::executors::executor { public: asio_executor(boost::asio::io_service &ios) : _ios(ios) {} void close() override { _ios.stop(); } bool closed() override { return _ios.stopped(); } void submit(work&& w) override { // for some reason _ios.post(w); causes endless // recursion and eventually a crash _ios.post(std::move(w)); } bool try_executing_one() override { return _ios.run_one() > 0; } private: boost::asio::io_service &_ios; };
Le 11/10/15 20:43, Mikael Olenfalk a écrit :
Hi,
I have stumbled on a minor inconvenience when it comes to using boost::future. Below is a small example which first wraps an asio io_service as an Executor (see even further below) and then uses boost::async() to launch a simple task on the executor and tries to queue up a continuation using future::then(Ex, F):
1. the destructor of async_result does not block which is nice Hmm, I believe that the in the develop branch tis should block. 2. the destructor of continuation_result does block which is a bit of an annoyance as this future I do not care about at all
Is there a way to make the destructor not block?
/M The current implementation of feature is broken (at least respect to what the standard TS Concurrency says) as the future resulting from ::then() should not blocks.
I have a branch (https://github.com/boostorg/thread/tree/feature/non_blocking_futures) that don't blocks on any future, but this will break async. I have another branch that has copyable executors and non-blocking futures (https://github.com/boostorg/thread/tree/feature/make_executors_copyable). Please, could you try on these branches and tell me how it works? Vicente
int main(int argc, char* argv[]) { boost::asio::io_service io_service; asio_executor executor{ io_service };
auto async_result = boost::async(executor, []() { return 42; }); // hangs: // async_result.then(executor, &g);
// does not hang: auto continuation_result = async_result.then(executor, [](boost::future<int> r) { cout << r.get() << endl; });
cout << "start io_service" << endl;
io_service.run();
cout << "stop io_service" << endl;
return 0; }
class asio_executor : public boost::executors::executor { public: asio_executor(boost::asio::io_service &ios) : _ios(ios) {}
void close() override { _ios.stop(); }
bool closed() override { return _ios.stopped(); }
void submit(work&& w) override { // for some reason _ios.post(w); causes endless // recursion and eventually a crash
_ios.post(std::move(w)); }
bool try_executing_one() override { return _ios.run_one() > 0; }
private: boost::asio::io_service &_ios; };
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Le 11/10/15 21:24, Vicente J. Botet Escriba a écrit :
Le 11/10/15 20:43, Mikael Olenfalk a écrit : The current implementation of feature is broken (at least respect to what the standard TS Concurrency says) as the future resulting from ::then() should not blocks.
I have a branch (https://github.com/boostorg/thread/tree/feature/non_blocking_futures) that don't blocks on any future, but this will break async. I have another branch that has copyable executors and non-blocking futures (https://github.com/boostorg/thread/tree/feature/make_executors_copyable).
BTW, I'm merging the fix to inherit the executor from the parent in these branches. So if you need this fix it woul dbe better to wait a little bit more. Vicente
participants (2)
-
Mikael Olenfalk
-
Vicente J. Botet Escriba