User Tools

Site Tools


c:c_threads:returning_values_from_a_thread

C - C++ Threads - Returning values from a Thread

Using std::future and std::promise.

A future is a data structure that represents some yet-undetermined result.

A promise is the provider of this result.

  • std::promise promises to set the value in its associated std::future object.
  • std::future provides a read-only view of a value.
    • The actual value is set directly by using a std::promise.
    • If somebody tries to access this associated value of future through get() function before it is available, then the get() function will block until the value is available.

Example 1

// promise example
#include <iostream>       // std::cout
#include <functional>     // std::ref
#include <thread>         // std::thread
#include <future>         // std::promise, std::future
 
void print_int (std::future<int>& fut) {
  int x = fut.get();
  std::cout << "value: " << x << '\n';
}
 
int main ()
{
  std::promise<int> prom;                      // Create promise.
 
  std::future<int> fut = prom.get_future();    // Engagement with future.
 
  std::thread th1 (print_int, std::ref(fut));  // Send future to new thread.
 
  prom.set_value (10);                         // Fulfill promise.
                                               // (synchronizes with getting the future).
  th1.join();
 
  return 0;
}

Example 2

#include <iostream>
#include <thread>
#include <future>
 
void initializer(std::promise<int> * promObj)
{
  std::cout << "Inside Thread" << std::endl;
  promObj->set_value(35);
}
 
int main()
{
  std::promise<int> promiseObj;
  std::future<int> futureObj = promiseObj.get_future();
  std::thread th(initializer, &promiseObj);
  std::cout << futureObj.get() << std::endl;
  th.join();
  return 0;
}    

NOTE: If the std::promise object is destroyed before setting the value the calling get() function on associated std::future object will throw an exception.

Apart from this, if you want your thread to return multiple values at different point of time then just pass multiple std::promise objects in the thread and fetch multiple return values from their associated multiple std::future objects.


Example 3

#include <vector>
#include <thread>
#include <future>
#include <numeric>
#include <iostream>
 
void accumulate(std::vector<int>::iterator first,
                std::vector<int>::iterator last,
                std::promise<int> accumulate_promise) {
  int sum = std::accumulate(first, last, 0);
  accumulate_promise.set_value(sum);  // Notify future
}
 
 
int main() {
  std::vector<int> numbers = { 1, 2, 3, 4, 5, 6 };
 
  //create promise
  std::promise<int> accumulate_promise;
 
  //engagement with future
  std::future<int> accumulate_future = accumulate_promise.get_future();
 
  std::thread work_thread(accumulate, numbers.begin(), 
                          numbers.end(), std::move(accumulate_promise));
 
  accumulate_future.wait();           // wait for result
  std::cout << "result=" << accumulate_future.get() << '\n';
  work_thread.join();                 // wait for thread completion
}

References

c/c_threads/returning_values_from_a_thread.txt · Last modified: 2021/03/05 22:37 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki