DonBaka
DonBaka

Reputation: 455

std::future in simple words?

I saw the uses of std::future in the program written in C++. So, I quickly went to lookup what is is: std::future and got a quite complicated answer for me.

Can someone put it in simple words for a 6 years old kid? I have an understanding level of a 6 years old kid...

Just a small definition with a minimal use case will work for me. Let me know if it is too much to ask and I'll take back my question.

Upvotes: 9

Views: 13127

Answers (3)

zkoza
zkoza

Reputation: 2869

std::future is used only in multithreaded programs. Such programs use threads, which can be thought of as mutually independent subprograms, to perform some tasks. For a 6-years child, this would be like when several people do something independently to achieve some task, e.g. mommy buys tickets while daddy books a hotel. Now, these "threads", "workers", "subprograms" need to communicate to share / transfer some data securely. The problem is that since they are completely independent, one needs a mechanism with which the "producer" of the information can either send it to the "consumer" or communicate its failure to produce the data; similarly, the "consumer" or "receiver" must have a secure, reliable way to tell whether a) the data from another thread is ready b) receive the data and c) receive the error signal should the "producer" fail to produce the data.

One method of providing such synchronization mechanism is a promise-future pair. They always go in pairs - if you can see std::future without std::promise, it's because the latter has been hidden from you, but it's somewhere. std::promise is used by the producer to set (and "send") the data, whereas std::future is used by "consumer" to receive it. If the producer cannot fulfil the contract, that is, if it cannot produce or "satisfy" the promise with the data, it may satisfy it with an exception, which is a C++ method of telling that something has gone wrong. Then, the "consumer" will receive this exception instead of data via its std::future.

In summary: std::future is an object used in multithreaded programming to receive data or an exception from a different thread; it is one end of a single-use, one-way communication channel between two threads, std::promise object being the other end. Using the promise-future pattern, you have a guarantee that your inter-thread communication is free of common errors specific to multithreaded programs, like race conditions or accessing already freed memory, which are very common if the threads "talk to each other" using methods designed for single-threaded programs, like when function (=thread) arguments are being passed by reference.

Upvotes: 7

user17732522
user17732522

Reputation: 76859

There is a quite simple example on the cppreference page you linked. Slightly modified:

std::future<int> f = std::async(std::launch::async, []{
    // long calculation
    return /* some result */;
});
/* do some other stuff */
int result = f.get();

std::async with the std::launch::async flag runs a function (here a lambda) asynchronously (in another thread). It returns a std::future, which will eventually, when that function finishes, contain an int value.

While that function is running, we can go on and /* do some other stuff */.

When we need the result, we call f.get() which waits until the int value is available, meaning until the asynchronous function has finished, and then gives it to us. The value f.get() gives us is the function's return value which std::async stored in the std::future after the function returned.

Instead of std::async it may also be used with threads via std::promise or std::packaged_task.

Upvotes: 13

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154025

A std::future<T> is a handle to a result of work which is [potentially] not, yet, computed. You can imagine it as the receipt you get when you ask for work and the receipt is used to get the result back. For example, you may bring a bike to bike store for repair. You get a receipt to get back your bike. While the work is in progress (the bike being repaired) you can go about other business. Eventually, when the work is supposed to be done you ask for the result (the repaired bike) and [potentially] wait until the result is delivered.

std::future<T> is a general mechanism to receive result. It doesn't prescribe how the requested work is actually executed.

Upvotes: 22

Related Questions