# C++11 Multithreading – Part 8: std::future , std::promise and Returning values from Thread

In the previous article, we have discussed about C++11 : Start thread by member function with arguments. Let us learn how to Multithreading – Part 8 in C++ Program.

## Multithreading – Part 8: std::future, std::promise and Returning values from Thread

The std :: future item can be used with asych, std :: packaged_task and std :: promise. By considering this article we will focus on using std :: future with std ::promise object .

Many times we encounter a situation where we want the cord to return the result.

Now the question is how can you do this?

Let’s take an example,

Suppose in our app we created a thread that will compress a given folder and we want this thread to retrieve a new zip file name and its size as a result.

Now to do this we have 2 ways,

• ### Old Way : Share data among threads using pointer :

Transfer the pointer to a new thread and this thread will set the data on it. Until there is a big thread keep waiting using the flexibility. When a new thread sets the data and signs the status quo, a large cable will wake up and retrieve the data from that identifier.

To make it easier we use variable, mutex and pointer i.e. 3 items to hold the returned value.

Now let’s say we want this thread to bring back 3 different values ​​at a different time when the problem will be more severe. Could there be a simple solution to recover value from threads.

The answer is yes using the std :: future, checkout solution following it.

• ### C++11 Way : Using std::future and std::promise

std :: future A category template and its object saves the future value.

Now what the hell is this future value.

In fact the std :: object of the future inside holds the value to be allocated in the future and also provides a way to achieve that value i.e. using the get member function (). But if someone tries to reach this corresponding future value by using the get () function before it is available, then the get () function will block until the value is not found.

std :: promise is also a class template and its object promises to set a value in the future. Each std ::promise  item has a std ::future  compatible item that will provide a value once set by the std :: promise:.

The object of std :: promise is sharing the details with its corresponding std ::future .

Let’s see step by step,

Create a std ::promise object that promises to Thread1.

std::promise<int> promiseObj;

Currently this promise item has no corresponding value. But it does promise that someone will put a value on it again

if  its set we can get that value by using the corresponding object std :: future.

But now let’s say that Thread 1 created this promise thing and passed it on to Thread 2. Now how can Thread 1 know that when Thread 2 will set a price on this promise thing?

Answer of this statement is using std::future object.

Everything std :: promise has a corresponding object called std :: future, where others can claim the value set by the promise.

Therefore, string 1 will create std ::promise object and download std :: future to it before transferring std ”” which promises string 2 i.e.

std::future<int> futureObj = promiseObj.get_future();

Now thread 1 will pass the promise Object to Thread 2.

After that Thread 1 will fetch the value set by Thread 2  to std :: promise by std :: future’s get function.

int ob = futureObj.get();

But if the value is not set by string 2 then this call will be blocked until string 2 sets the value in the promised item i.e.

promiseObj.set_value(51);

See full flow in the given drawing,

Let’s see a complete example of std :: future and std ::promise,

#include <iostream>

#include <thread>

#include <future>

void demo(std::promise<int> * promItem)

{

std::cout<<"The inside Thread is:"<<std::endl;     promItem->set_value(51);

}

int main()

{

std::promise<int> promiseItem;

std::future<int> futureItem = promiseItem.get_future();

std::thread th(demo, &promiseItem);

std::cout<<futureItem.get()<<std::endl;

th.join();

return 0;

}
Output :
The inside Thread is:
51

If the std ::promise object is used before setting the value of the call function get () in relation to the std ::future object the next item will discard the alternative.

The part from this, if you want your thread to retrieve multiple values ​​at a different time then simply transfer a lot of std :: promise items and download a lot of retrieval from many std :: future related items.