1

When I debug a std::future<T> future I see that visual studio shows the value as pending, and this makes sense, the work has not started, (in most cases).

And, there are no guarenties that it will start any time soon.

After the work is complete, the 'value' changes to has_value.

I understand that this is just a debug window, but I am currious on how I could get the same value in my code to use it.

How can I tell what the status of the future using c++17? What I am specifically after is, telling if the future has started / running / finished.

This is how I start the future, auto future = std::async(std::launch::async, ...);

I know I could use flags in my code that would tell me if it has started or not, but I am currious if there is a more standard way of doing it.

VS2019 future status

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
FFMG
  • 1,208
  • 1
  • 10
  • 24
  • 3
    "I see that visual studio shows the value as pending, and this makes sense, the work has not started, (in most cases)." -- why do you think the work has not started on a future you have launched as async? – Yakk - Adam Nevraumont Dec 03 '19 at 16:01
  • I can tell that e function has not started because I have trace messages. Also, why else would VS show `pending` and `has_value`, what would be the point of those messages – FFMG Dec 03 '19 at 16:49
  • The thread "spooling up" -- getting created, scheduled, etc -- is part of the "work" in a sense. I'm unaware of an implementation that doesn't start that right away, hence my surprise. – Yakk - Adam Nevraumont Dec 03 '19 at 16:54

2 Answers2

0

You could get the same kind of result by calling future.wait_for(std::chrono::seconds(0)), which returns a future_status enum.

I'm guessing the debugger shows pending for a result of future_status::deferred.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • No, sorry, if you run `future.wait_for(std::chrono::seconds(0))` on a `pending` future you get a `future_status::timeout`. So I am guessing it is something else. – FFMG Dec 03 '19 at 09:32
0

As far as I concerned, we could check the state of the std::future by callde wait_for with a 0 duration, and check the result code that can be one of future_status::ready, future_status::deferred or future_status::timeout. For exemple:

my_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready

If the function is deferred this will never return true, so it's probably better to check wait_for directly in the case where you might want to run the deferred task synchronously after a certain time has passed or when system load is low.

I suggest you could refer to the link:Is there a way to check if std::future state is ready in a guaranteed wait-free manner?

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20
  • Thanks, but this only tells me if a future is complete or not. I was wondering if there was a way of knowing if it has started at all. While not the same thing, c# has something similar, (https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskstatus?view=netframework-4.8) with `WaitingForActivation`, `WaitingToRun`, `Running`, `RanToCompletion` and so on. I am basically looking for a way to know if a thread has been started to run that future or if it is pending. – FFMG Dec 04 '19 at 11:52
  • [std::future_status](https://en.cppreference.com/w/cpp/thread/future_status),with only 3 states:ready, timeout, deferred. As far as I'm concerned we couldn't get a way to know if a thread has been started to run that future or if it is pending. – Jeaninez - MSFT Dec 05 '19 at 03:15