std::future and std::promise

std::future and std::promise

These are the ways to pass data from one thread to another thread. While this can also be done with other capabilities, such as global variables, Old way - share data among threads using pointers, std::future and std::promise works without them 
  • promise is non-copyable. One can move it, but cannot copy it.
  • future is also non-copyable, but a future can become a shared_future that is copyable. So you can have multiple destinations, but only one source.
  • promise can only set the value, it can't even get it back.
  • future can only get the value, it cannot set it.

Old Way – Share data among threads using pointers.

Pass a pointer to the new thread and this thread will set data in it. Till then in main thread keep on waiting using a condition variable. When new thread sets the data an signals the condition variable, then main thread will wake up and fetch data from that pointer.
To do a simple thing we used a condition variable, a mutex and a pointer i.e, 3 items to catch a returned value.

C++11 way : std::future and std::promise.

std::future is a class template and its object stores the future value.
std::future object internally stores a value that will be assigned in future and it also provides a mechanism to access than value i.e, using get() member function. But if somebody tries to access the associated value of future through get() function before it is available, then get() function will block till value is not available. 
std::promise is also a class template and its object promises to set the value in future. Each std::promise object has an associated std::future object that will give value once set by the std::promise object.
std::promise object shares data with its associated std::future object.

Step by step.

1. Create a std::promise object in Thread1.
        std::promise<int> promiseObj;
As of now this promise object doesn’t have any associated value. But it gives a promise that somebody will surely set the value in it and once it is set then you can get that value through associated std::future object.

2. Fetch the future object from promiseObj before passing it to Thread 2.
        std::future<int> futureObj = promiseObj.get_future();

3. Now Thread 1 will pass the promiseObj to Thread 2.
        std::thread t(functionName, &std::move(promiseObj));

4. Now thread 2 will set the value in promiseObj.
        promiseObj.set_value(50);

5. Then Thread 1 can fetch the value set by Thread 2 n std::promise through std::future’s get function.
        int val = futureObj.get();
But if value is not set by the Thread 2 then this call will get blocked until Thread 2 sets the value in promise object.


#include <future>
#include <iostream>
#include <thread>
#include <utility>

void multiplier(std::promise<int>&& intPromise, int a, int b){
  intPromise.set_value(a*b);
}

void divider() (std::promise<int>&& intPromise, int a, int b) {
    intPromise.set_value(a/b);
  }


int main(){

  int a= 20;
  int b= 10;

  std::cout << std::endl;

  // define the promises
  std::promise<int> prodPromise;
  std::promise<int> divPromise;

  // get the futures
  std::future<int> prodFutureResult= prodPromise.get_future();
  std::future<int> divFutureResult= divPromise.get_future();

  // calculate the result in a separat thread
  std::thread prodThread(multiplier,std::move(prodPromise),a,b);
 
  std::thread divThread(divider,std::move(divPromise),a,b);

  // get the result
  std::cout << "20*10= " << prodFutureResult.get() << std::endl;
  std::cout << "20/10= " << divFutureResult.get() << std::endl;

  prodThread.join();
 
  divThread.join();

  std::cout << std::endl;

}


Output –
20*10= 200
20/10= 2



Comments

Popular posts from this blog

C++ Guidelines for Multithreaded System

Signalling System #7(SS7) Protocol.

std::shared_ptr