【问题标题】:std::promise and std::future in c++c++ 中的 std::promise 和 std::future
【发布时间】:2014-11-10 19:01:49
【问题描述】:

std::promise 提供了一种设置值(类型 T)的方法,它 稍后可以通过关联的 std::future 对象读取

  1. 这两者究竟是如何关联的?

  2. 我担心未来会与错误的承诺配对是否合理?

更新:来自并发操作的示例...(但代码无法编译)

#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);
            }
        }
    }
}

【问题讨论】:

标签: c++ multithreading


【解决方案1】:
  1. promisefuture 视为创建数据的一次性通道。 promise 创建通道,并最终使用promise::set_value 将数据写入通道。 future 连接到通道,future::wait 在数据写入后读取并返回数据。

  2. 不用担心,因为将futurepromise“配对”的唯一方法是与promise::get_future

【讨论】:

  • 我已经更新了帖子。示例中没有包含 promise::get_future。
  • @Decipher 那是因为没有使用future。没有futurepromise 就毫无用处。 (就像生活一样!)
  • @Decipher 看future的定义。公开的构造函数无法让 future 值得等待——默认构造函数基本上只存在于您想稍后为其分配一些其他 future 的情况下。如果你想要future,你需要从生产者那里得到它,比如promise
【解决方案2】:
  1. 它们由std::promise::get_future 成员函数关联。您可以通过调用此函数获得与std::promise 关联的std::future

    std::future 表示您尚未拥有但最终会拥有的值。它提供了检查值是否可用或等待它可用的功能。

    std::promise 承诺您最终会设置一个值。当一个值最终被设置时,它将通过其对应的std::future 提供。

  2. 不,因为您不会在创建后将它们配对。您从 std::promise 获得您的 std::future,因此它们具有内在联系。

【讨论】:

    【解决方案3】:

    std::promise&lt;class T&gt; promiseObj;

    1. promise 对象创建一个可以存储 T 类型值的容器

    std::future&lt;class T&gt; futureObj = promiseObj.get_future();

    1. future 对象一旦持有某个值,就会检索由 Promise 对象创建的容器中的内容。一个future对象需要与promise对象创建的容器相关联,这可以通过上面的sn-p来完成。 因此,如果您将未来与预期的承诺对象相关联,那么未来不应该与错误的承诺对象配对。

    2. 这是一个示例程序,可以清楚地说明未来承诺的用法:

      #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;
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-14
      • 2017-10-12
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多