What is std::promise?

Kerrek SB

I'm fairly familiar with C++11's std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward.

However, I cannot quite grasp what std::promise is, what it does and in which situations it is best used. The standard document itself doesn't contain a whole lot of information beyond its class synopsis, and neither does just::thread.

Could someone please give a brief, succinct example of a situation where an std::promise is needed and where it is the most idiomatic solution?

Jonathan Wakely

In the words of [futures.state] a std::future is an asynchronous return object ("an object that reads results from a shared state") and a std::promise is an asynchronous provider ("an object that provides a result to a shared state") i.e. a promise is the thing that you set a result on, so that you can get it from the associated future.

The asynchronous provider is what initially creates the shared state that a future refers to. std::promise is one type of asynchronous provider, std::packaged_task is another, and the internal detail of std::async is another. Each of those can create a shared state and give you a std::future that shares that state, and can make the state ready.

std::async is a higher-level convenience utility that gives you an asynchronous result object and internally takes care of creating the asynchronous provider and making the shared state ready when the task completes. You could emulate it with a std::packaged_task (or std::bind and a std::promise) and a std::thread but it's safer and easier to use std::async.

std::promise is a bit lower-level, for when you want to pass an asynchronous result to the future, but the code that makes the result ready cannot be wrapped up in a single function suitable for passing to std::async. For example, you might have an array of several promises and associated futures and have a single thread which does several calculations and sets a result on each promise. async would only allow you to return a single result, to return several you would need to call async several times, which might waste resources.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

std::async vs std::promise

From Java

Why are std::future and std::promise not final?

From Dev

Unable to use std::bind with std::promise

From Dev

What is the promise disposer pattern?

From Dev

What is a promise object?

From Dev

What is a parent promise?

From Dev

What is a promise object?

From Dev

What is Promise in javascript?

From Dev

What is the difference between javascript Promise and q promise

From Dev

What is the use case of Promise.resolve(promise)?

From Dev

How to fix error 'std::promise<int>::promise(const std::promise<int> &)' : attempting to reference a deleted function

From Dev

How to fix error 'std::promise<int>::promise(const std::promise<int> &)' : attempting to reference a deleted function

From Dev

What is a function that returns a promise called?

From Dev

what is the "f" mean in angular promise?

From Dev

In what order are Promise callbacks triggered?

From Dev

What is the purpose of the nested promise in this code?

From Dev

What's "this" inside a promise creation?

From Dev

C++11 Segmentation fault with std::promise

From Dev

Capture std::promise in a lambda C++14

From Dev

C++11 Segmentation fault with std::promise

From Dev

Lifetime issues of std::promise in an async API

From Java

What is the purpose of std::launder?

From Java

What exactly is std::labs() there for?

From Dev

What is the performance of std::bitset?

From Dev

What is the complexity of std::tgamma?

From Java

What exactly is std::atomic?

From Dev

What happened to std::assert

From Dev

What is the precision of std::erf?

From Dev

What is std::strtoul for unicode