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
21
votes
1 answer

behaviour of std::async(std::launch::deferred) + std::future::then

The idea behind a deferred future (achieved only by calling std::async with std::launch::deferred flag) is that the callback is called only when someone tries to wait or to pull the futuristic value or exception of the future. by then the callback…
David Haim
  • 25,446
  • 3
  • 44
  • 78
14
votes
3 answers

Lazy evaluation in C++14/17 - just lambdas or also futures etc.?

I just read: Lazy Evaluation in C++ and noticed it's kind of old and most of the answers regard pre-2011 C++. These days we have syntactic lambdas, which can even deduce the return type, so lazy evaluation seems to boil down to just passing them…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
10
votes
3 answers

Is there a way to check if std::future state is ready in a guaranteed wait-free manner?

I know that I can check the state of the std::future the following way: my_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready But according to cppreference.com std::future::wait_for may block in some cases: This function may…
Alexandre A.
  • 1,619
  • 18
  • 29
8
votes
3 answers

Why std::future is different returned from std::packaged_task and std::async?

I got to know the reason that future returned from std::async has some special shared state through which wait on returned future happened in the destructor of future. But when we use std::pakaged_task, its future does not exhibit the same…
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
8
votes
1 answer

Does std::promise internally use std::condition_variable to notify the associated std::future?

My question is does std::promise notify the associated std::future through using a std::condition_variable? I search the source code of std::promise and found this website. But I didn't see std::promise has std::condition_variable in its member…
Tes
  • 349
  • 3
  • 12
7
votes
1 answer

How to create an already-resolved future

I have a function that returns a std::future. I have added a cache to the implementation, and I would like to optionally return a value immediately if it does not need to be recalculated. How can I create an already-resolved future? // Class…
Carson
  • 2,700
  • 11
  • 24
7
votes
1 answer

How to adapt a C++ std::future return value to a C# System.Threading.Tasks.Task?

I am wrapping a C++ library to be used from .NET. There are functions in the C++ API that return std::future. I want to have the .NET proxy classes return System.Threading.Tasks.Task. I thought of adding a replacement method on the C++ side that…
shmuelie
  • 1,205
  • 1
  • 9
  • 24
7
votes
1 answer

Forcibly terminate method after a certain amount of time

Say I have a function whose prototype looks like this, belonging to class container_class: std::vector container_class::func(int param); The function may or may not cause an infinite loop on certain inputs; it is impossible to tell which…
ack
  • 1,181
  • 1
  • 17
  • 36
7
votes
1 answer

How does std::future affects the lifetime of an associated std::packaged_task?

I have an std::packaged_task containing a lambda that captures a variable by copy. When this std::packaged_task is deleted, I would expect the variable living inside the lambda to be destructed, but I noticed that if I get the associated std::future…
6
votes
1 answer

std::futures and exception

Given the following source code #include #include #include #include #include int main() { auto task = std::async(std::launch::async, [] { …
IcePic
  • 293
  • 1
  • 10
4
votes
1 answer

C++ cannot call set_value for promise move-captured in a lambda?

I'm trying to write a fairly simple method that returns a future. A lambda sets the future. This is a minimal example. In reality the lambda might be invoked in a different thread, etc. #include std::future do_something()…
MHebes
  • 2,290
  • 1
  • 16
  • 29
4
votes
1 answer

error: no member named 'async' in namespace 'std'

I know that std::async is a C++11 thing but I am pretty sure that my compiler has C++11 support. #include using namespace::std; void functionToRun() { // some code } int main() { auto x = 2; // throws warning warning: 'auto' type…
coda
  • 2,188
  • 2
  • 22
  • 26
4
votes
1 answer

Is it thread-safe to declare a std::future class member as being mutable?

Is it safe (thread-safe) to declare a std::future as being mutable since its get() function changes its state. I assume that it's like std::mutex which is safe to be made mutable. template struct S { void query() { m_fut =…
James
  • 9,064
  • 3
  • 31
  • 49
4
votes
1 answer

Generate a promise from a template Callable

I'm trying to create a promise from a template function which accepts a Callable type. But I'm not sure how to do it. I tried using std::invoke_result_t, but that needs the arguments to know the result type, something which I won't know when I…
user3666471
  • 907
  • 11
  • 24
3
votes
0 answers

Can code flow move ahead before future get method returns?

Is it possible that "Main end" could get displayed before all result.get(); are returned back in below code snippet (Under any scenario)? OR "Main end" will always be the last one to appear? #include #include #include…
Helena
  • 444
  • 2
  • 15
1
2 3 4 5