Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't wait on a wrapped future #2370

Closed
stevenrbrandt opened this issue Oct 26, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@stevenrbrandt
Copy link
Member

commented Oct 26, 2016

I create a simple wrapped future in the attached file. It uses a sleep(1) to ensure that the lambda function doesn't complete too quickly to see the probem. The problem is that the wait() call completes, before the wrapped future finishes, thus the code prints:

finish
I am done

instead of:

I am done
finish

The way things stand, there seems to be no way to wait on a wrapped future.

@hkaiser

This comment has been minimized.

Copy link
Member

commented Oct 26, 2016

Here is the smallest possible use case reproducing the issue:

#include <hpx/hpx.hpp>
#include <hpx/hpx_main.hpp>

#include <iostream>
#include <chrono>

hpx::future<void> run() {
  return hpx::async([](){
    hpx::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "I am done" << std::endl;
    return hpx::make_ready_future();
  });
}

int main(int argc,char **argv) {
  hpx::async(run).wait();
  std::cout << "finish" << std::endl;
  return 0;
}

@hkaiser hkaiser added this to the 1.0.0 milestone Oct 26, 2016

@stevenrbrandt

This comment has been minimized.

Copy link
Member Author

commented Oct 26, 2016

I thought I'd attached that code. Apparently the web interface tricked me.

@hkaiser

This comment has been minimized.

Copy link
Member

commented Oct 27, 2016

The code does the correct thing. The problem is that async(run) returns a future<future<void>>, so that async(run).wait() will just wait for the outer future to become ready. The outer future however becomes ready when run() has executed to completion. In order to achieve the effect you are looking for you'd need to change the code to:

async(run).get().wait();

or alternatively (forcing implicit unwrapping):

future<void> f = async(run); f.wait();

I hope that explains things.

@hkaiser hkaiser closed this Oct 27, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.