简体   繁体   中英

std::promise and std::future in c++

std::promise provides a means of setting a value (of type T), which can later be read through an associated std::future object

  1. How exactly these two are associated?

  2. Is my concern reasonable that the future would pair with the wrong promise?

update: example from concurrency in action... (the code cannot compile, though)

#include <future>
void process_connections(connection_set& connections)
{
    while(!done(connections)){
        for(connection_iterator
        connection=connections.begin(),end=connections.end();
        connection!=end;
        ++connection)
        {
            if(connection->has_incoming_data()){
                data_packet data=connection->incoming();
                std::promise<payload_type>& p=
                connection->get_promise(data.id);
                p.set_value(data.payload);
            }
            if(connection->has_outgoing_data()){
                outgoing_packet data=
                connection->top_of_outgoing_queue();
                connection->send(data.payload);
                data.promise.set_value(true);
            }
        }
    }
}
  1. Think of promise and future as creating a single-use channel for data. promise creates the channel, and eventually writes the data to it with promise::set_value . future connects to the channel, and future::wait reads and returns the data once it's been written.

  2. No real concern, because the only way to "pair" a future with a promise is with promise::get_future .

  1. They are associated by the std::promise::get_future member function. You get the std::future associated with an std::promise by calling this function.

    A std::future represents a value that you do not yet have, but will have eventually. It provides functionality to check whether the value is available yet, or to wait for it to be available.

    A std::promise makes a promise that you will eventually set a value. When a value is eventually set, it will be made available through its corresponding std::future .

  2. No, because you don't pair them after creation. You get your std::future from a std::promise , so they are inherently linked.

std::promise<class T> promiseObj;

  1. A promise object creates a container that can store a value of type T

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

  1. A future object retrieves what is there in container created by promise object as soon as it holds some value. A future object needs to be associated with the container created by promise object, this can be done by the above snippet. So if you have associated future with intended promise object, it should not be the case that future gets paired with the wrong promise object.

  2. Here is an example program that makes the future-promise usage clear:

     #include <iostream> #include <thread> #include <future> //Some Class will complex functions that you want to do in parallel running thread class MyClass { public: static void add(int a, int b, std::promise<int> * promObj) { //Some complex calculations int c = a + b; //Set int c in container provided by promise promObj->set_value(c); } }; int main() { MyClass myclass; //Promise provides a container std::promise<int> promiseObj; //By future we can access the values in container created by promise std::future<int> futureObj = promiseObj.get_future(); //Init thread with function parameter of called function and pass promise object std::thread th(myclass.add, 7, 8, &promiseObj); //Detach thread th.detach(); //Get values from future object std::cout<<futureObj.get()<<std::endl; return 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM