Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse boost::promise object after getting its future value?

Tags:

c++

boost

void sss(boost::promise<std::string>& res)
{
   res.set_value("hi");
}

void yyy(boost::promise<std::string>& res)
{
  res.set_value("hello");
}

int main()
{
  boost::thread th;
  boost::promise<std::string> a;
  th = boost::thread(sss, boost::ref(a));
  th.join();
  std::cout << a.get_future().get() << std::endl;
  th = boost::thread(yyy, boost::ref(a));
  th.join();
  std::cout << a.get_future().get() << std::endl;
}`

I'm getting the error that the promise is already satisfied. How to reuse the same object of promise?

like image 700
Bakkya Avatar asked Jan 05 '15 15:01

Bakkya


1 Answers

Replace it with an unused promise:

a = boost::promise<std::string>();
like image 153
Mike Seymour Avatar answered Oct 27 '22 23:10

Mike Seymour