Boost logo

Boost :

Subject: [boost] [thread] Is there a non-blocking future-destructor?
From: Mikael Olenfalk (mikael.olenfalk_at_[hidden])
Date: 2015-10-11 14:43:03


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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk