[英]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
std :: promise提供了一种设置值(类型为T)的方法,稍后可以通过关联的std :: future对象读取该值
How exactly these two are associated? 这两者究竟如何相关联?
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);
}
}
}
}
Think of promise
and future
as creating a single-use channel for data. 将
promise
和future
想象成为数据创建一次性使用渠道。 promise
creates the channel, and eventually writes the data to it with promise::set_value
. promise
创建通道,并最终使用promise::set_value
将数据写入其中。 future
connects to the channel, and future::wait
reads and returns the data once it's been written. future
连接到channel, future::wait
读取并返回数据。
No real concern, because the only way to "pair" a future
with a promise
is with promise::get_future
. 没有真正关心,因为将
future
与promise
“配对”的唯一方法是使用promise::get_future
。
They are associated by the std::promise::get_future
member function. 它们由
std::promise::get_future
成员函数关联。 You get the std::future
associated with an std::promise
by calling this function. 通过调用此函数,您可以获得与
std::promise
关联的std::future
。
A std::future
represents a value that you do not yet have, but will have eventually. std::future
表示您尚未拥有但最终会拥有的值。 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. std::promise
承诺最终会设置一个值。 When a value is eventually set, it will be made available through its corresponding std::future
. 当最终设置一个值时,它将通过其相应的
std::future
。
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
得到你的std::future
,所以它们本身就是联系在一起的。
std::promise<class T> promiseObj;
std::future<class T> futureObj = promiseObj.get_future();
A future object retrieves what is there in container created by promise object as soon as it holds some value. 未来对象一旦保存了某个值,就会检索promise对象创建的容器中的内容。 A future object needs to be associated with the container created by promise object, this can be done by the above snippet.
未来的对象需要与promise对象创建的容器相关联,这可以通过上面的代码片段完成。 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.
因此,如果您将未来与预期的promise对象关联起来,那么未来不应该与错误的promise对象配对。
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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.