c++ - What is std::promise? -


i'm familiar new standard library's std::thread, std::async , std::future components (e.g. see this answer), straight-forward.

however, cannot quite grasp std::promise is, , in situations best used. standard document doesn't contain whole lot of information beyond class synopsis, , neither just::thread.

could please give brief, succinct example of situation std::promise needed , idiomatic solution?

in words of [futures.state] std::future asynchronous return object ("an object reads results shared state") , std::promise asynchronous provider ("an object provides result shared state") i.e. promise thing set result on, can get associated future.

the asynchronous provider creates shared state future refers to. std::promise 1 type of asynchronous provider, std::packaged_task another, , internal detail of std::async another. each of can create shared state , give std::future shares state, , can make state ready.

std::async higher-level convenience utility gives asynchronous result object , internally takes care of creating asynchronous provider , making shared state ready when task completes. emulate std::packaged_task (or std::bind , std::promise) , std::thread it's safer , easier use std::async.

std::promise bit lower-level, when want pass asynchronous result future, code makes result ready cannot wrapped in single function suitable passing std::async. example, might have array of several promises , associated futures , have single thread several calculations , sets result on each promise. async allow return single result, return several need call async several times, might waste resources.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -