LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   throw_future_error exception. (https://www.linuxquestions.org/questions/programming-9/throw_future_error-exception-4175627347/)

shamjs 04-09-2018 11:28 PM

throw_future_error exception.
 
Hi,

I am facing std::__throw_future_error(int) () exception if i try to do std::future<bool>::get() ()

Note: this exception is thrown sometimes (2 out of 10 times)

here is my code snippet.

Code:


MyClass::MyClass()
{
  futureNotification = prom.get_future();
}

void* thread_Fun(void *arg)
{
  MyClass *ptr = (MyClass *)arg;

  bool notification = ptr->GetFutureNotification();
 
  if(notification)
  {
    // do relevant work.
  }
}

bool MyClass::GetFutureNotification()
{
  return futureNotification.get();
}

void MyClass::SetNotification()
{
  prom.set_value(true);
}


class MyClass
{
    std::promise<bool> prom;
    std::future<bool> futureNotification;

  public :

    void SetNotification(); 
    bool GetFutureNotification(); 

    ...
};

Can anyone let me know why the exception is thrown? Also why this exception is thrown sometimes and not always?

Note : i set the notification (SetNotification) from my other module of program.

My thread function (thread_Fun) will be waiting for notification from GetFutureNotification, when once the promise value is set the thread gets unblocked and proceed with its execution.

I appreciate your support in this regard.

sundialsvcs 04-10-2018 09:58 PM

Be sure that you don't have any sort of "race" between the code that sets the future and any other code that expects to assert it.


All times are GMT -5. The time now is 02:27 PM.