8
#include <iostream>
#include <future>
#include <chrono>

using namespace std;
using namespace std::chrono;

int sampleFunction(int a)
{
    return a;
}

int main()
{
   future<int> f1=async(launch::deferred,sampleFunction,10);
   future_status statusF1=f1.wait_for(seconds(10));
   if(statusF1==future_status::ready)
        cout<<"Future is ready"<<endl;
   else if (statusF1==future_status::timeout)
        cout<<"Timeout occurred"<<endl;
   else if (statusF1==future_status::deferred)
        cout<<"Task is deferred"<<endl;
   cout<<"Value : "<<f1.get()<<endl;
}

Output -
Timeout occurred
Value : 10

In above example, I was expecting future_status to be deferred instead of timeout. sampleFunction has been launched as launch::deferred. Hence it will not be executed until f1.get() has been called. In such condition wait_for should have returned future_status::deferred and not future_status::timeout.

Appreciate if someone can help me understand this. I am using g++ version 4.7.0 on fedora 17.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
tshah06
  • 2,625
  • 3
  • 19
  • 23
  • 9
    GCC and the supplied standard library does not fully implement all functionality of C++11 yet. See e.g. [here](http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html) for the status of the library. – Some programmer dude Aug 27 '12 at 07:51
  • from the page quoted by Joachim: "Class template future: Partial: Timed waiting functions do not return future_status". says it all – Walter Aug 27 '12 at 19:16
  • Voting to close, as the issue has no resolution. – Kerrek SB Sep 03 '12 at 10:43
  • Your example throws exception ``` terminate called after throwing an instance of 'std::system_error' what(): Unknown error -1 ``` on GCC 11.1, But success runs on clang 12.0 with libc++ – Khurshid Normuradov Jan 16 '23 at 13:47

1 Answers1

3

GCC and the GNU STL have no support of the complete C++ 11.

Here you can check out the C++ 11 implementation status in GCC and GNU STL:

http://gcc.gnu.org/projects/cxx0x.html

http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

Also, read this discussion thread: http://blog.gmane.org/gmane.comp.gcc.bugs/month=20120201

Sergey K.
  • 24,894
  • 13
  • 106
  • 174