get future from promise c++

Getting Future from Promise in C++

#include <iostream>
#include <future>

int main() {
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();

    // ... (do something with the promise and future)

    return 0;
}
  1. Include Necessary Libraries:
  2. #include <iostream>: for input/output stream.
  3. #include <future>: for working with promises and futures.

  4. Create Promise and Get Future:

  5. std::promise<int> prom;: Create a promise that can store a value of type int.
  6. std::future<int> fut = prom.get_future();: Get a future associated with the promise, which allows access to the value that will be set in the promise.

  7. Use the Promise and Future:

  8. After obtaining the future from the promise, it can be used to retrieve the value set by the promise at a later point in the program.
  9. The promise and future are typically used in a producer-consumer scenario, where one part of the program sets a value using the promise, and another part retrieves the value using the future.

  10. Return 0:

  11. The main() function returns 0 to indicate successful execution. ```