The most efficient way to delete both in Vim and Shell
The most efficient way to delete both in Vim and Shell
April 5, 2022
What is the Bootloader
What is the Bootloader?
April 19, 2022

April 12, 2022

How std::promise and std::future work

std::promise and std::future work as asynchronous handshake contract on an object. Whenever the acquired object value is obtained by promise, it sets the value and passes the result between threads. Future returns the retrieved value. Since this is an asynchronous operation, future is halted till a value is provided either certain amount of time or till it gets the result.

void Customer::get_total_loan (std::promise<int> &&promise, int key) 
{
  std::string name = customerMap.find(key) ->second; 
  auto totalLoan = Customer::read_from_file (name);
  std::this_thread::sleep_for(std::chrono:: seconds(5));
  promise.set_value(totalLoan);
}

int main() {
  std::promise<int> promise;
  std::future<int> future = promise.get_future();
  std::thread th(get_total_loan, std::move (promise), 12);
  future.wait();
  std::cout<<"Total loan of the customer: "<<future.get(); 
  th.join();
}
How std::promise and std::future work
This website uses cookies to improve your experience. By using this website you agree to our Data Privacy Statement
Read more