Questions tagged [std-future]

The [c++11] std::future object can be used to retrieve the result of the asynchronous operations or any exceptions it throws.

An asynchronous operation can be created via:

  • std::async
  • std::packaged_task
  • std::promise

Each of the above can provide a std::future object to the creator of that operation.

A std::future object (let name it f) can be used to

  • wait for the result: f.wait(), f.wait_for(duration), f.wait_until(time_point)
  • retrieve the value (if ready): f.get()
  • share its state (std::shared_future): f.share(), f.valid()

More here.

62 questions
3
votes
1 answer

std::future get() blocks when wait_for() status is ready and wait() returns

I expect the below code to pass all assertions and complete successfully every time. Currently it seems std::future.get() blocks in both branches everytime. It blocks forever despite wait_for() showing the status as ready and wait() returning…
user2183336
  • 706
  • 8
  • 19
3
votes
1 answer

std::promise set_exception twice cause Segmentation fault

Let's say I have a method that calls an unstable third-party service, so I add a timeout for this call say 10 seconds. Here is what I tried: int process() { std::promise promise; std::future future = promise.get_future(); …
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
3
votes
2 answers

Can I execute on get my `std::future` and wait on it too?

So you can create a std::future that does no work until .get() is called: auto f_deferred = std::async( std::launch::deferred, []{ std::cout << "I ran\n"; } ); You can also write a std::future that is waitable, and can be made ready at any point by…
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
2
votes
0 answers

Is it acceptable to put std::promise-s into a container to be set by the thread?

There is a thread which is responsible for executing a certain task and acquire the result. To execute the task certain data is needed and it is being produced by multiple clients and the result must be returned to them. The solution that I am…
2
votes
1 answer

Boost asio:async_read() using boost::asio::use_future

When calling asio::async_read() using a future, is there a way to get the number of bytes transferred when a boost:asio::error::eof exception occurs? It would seem that there are many cases when one would want to get the data transferred even if…
2
votes
1 answer

How to use std future and async with threading in a for loop with a shared resource as param?

I got stuck with an implementation problem in my threading practice project. I know what I want to achieve but I dont know how. I am new in the topic of std::future and std::async so I am not even sure if the implementation is even possible in the…
Seraph
  • 43
  • 4
2
votes
0 answers

C++ std::future hangs on timeout

All I have setup a test project for learning std::future and its use with std::async. It is a simple console app and the code is as follows #include #include #include using namespace std; std::string getLine() { …
sramalingam24
  • 1,297
  • 1
  • 14
  • 19
2
votes
2 answers

use_count() of shared_ptr moved into a std::async in gcc 4.6.3

In the code below, I want the use_count() of the shared_ptr moved into the std::async to be 1: #include #include #include using namespace std; void fun(shared_ptr sp) { cout << "fun: sp.use_count() == " <<…
Quokka
  • 174
  • 1
  • 3
  • 10
2
votes
4 answers

std::future returned from std::async hangs while going out of scope

I am using a combination of std::async and std::future from C++ 11. I am using to enforce a time_out on a certain activity that I do in my code which might take time as I try connecting to server. Following is how the code is: #include…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
1
vote
0 answers

std::future is set to 'empty' state c++

The sr.tFuture is initialized with 'empty' state instead of 'pending' and I found out when I remove tFuture() from the constructor initialization list, the state of sr.tFuture becomes pending which is correct. But I don't quite understand the…
Sami
  • 513
  • 4
  • 11
1
vote
1 answer

How to tell std::future if std::promise exist in C++?

I create a std::packaged_task, push it to a std::list and return its std::future to users, like this: future_t emplace(...) { //... auto task = std::make_shared>( std::bind(std::forward(f),…
BBing
  • 152
  • 5
1
vote
0 answers

C++11 getting std::future data updated from a thread

I have a udp socket running on a separate thread. I need to get certain data only periodically, something like: void runThread(udpSocketObj s, std::promise* f1, std::promise* f2) { s->rec(ret); ... f1->set_value(someValue in…
DHT201703
  • 11
  • 2
1
vote
0 answers

How do you properly Await/Await Async in C++?

Sorry if the question is not clear. i'll try to explain it here. I am working on a test project where two nodes will be communicating specialty packets to each other. As in: Node A will be sending a packet to Node B, and while Node B is generating…
1
vote
1 answer

C++ storing std::future form std::async in vector and wating for all

I want to do several tasks in parallel with std::async and then wait until all the futures have completed. void update() { // some code here } int main() { std::vector> handles(5); for (int i = 0; i < 5; ++i) { auto…
Symlink
  • 383
  • 2
  • 12
1
vote
2 answers

Measure time until an std::future object becomes available

I am trying to measure the duration of an API call which returns an std::future object. My current approach looks like this: std::chrono::high_resolution_clock::time_point endTime, startTime =…
fabmene
  • 55
  • 6