繁体   English   中英

什么是 std::promise?

[英]What is std::promise?

我非常熟悉 C++11 的std::threadstd::asyncstd::future组件(例如,请参阅此答案),它们非常简单。

但是,我不太明白std::promise是什么,它做什么以及在什么情况下最好使用它。 标准文档本身不包含超出其类概要的大量信息, std::thread也不包含。

有人可以给出一个简短的例子,说明需要std::promise的情况以及它是最惯用的解决方案吗?

我现在对情况有了更好的了解(由于这里的答案,数量不多)。 所以我想我自己写一点。


C++11 中有两个不同但相关的概念:异步计算(在别处调用的函数)和并发执行(线程,同时工作的东西)。 这两者是有些正交的概念。 异步计算只是函数调用的一种不同形式,而线程是一个执行上下文。 线程本身很有用,但出于本次讨论的目的,我将把它们视为实现细节。


异步计算有一个抽象层次。 例如,假设我们有一个接受一些参数的函数:

int foo(double, char, bool);

首先,我们有模板std::future<T> ,它表示T类型的未来值。 该值可以通过成员函数get()检索,它通过等待结果有效地同步程序。 或者,未来支持wait_for() ,可用于探测结果是否已经可用。 Futures 应该被认为是普通返回类型的异步替代品。 对于我们的示例函数,我们期望一个std::future<int>

现在,进入层次结构,从最高层到最低层:

  1. std::async :执行异步计算最方便直接的方法是通过async函数模板,它会立即返回匹配的未来:

     auto fut = std::async(foo, 1.5, 'x', false); // is a std::future<int>

    我们几乎无法控制细节。 特别是,我们甚至不知道该函数是并发执行的、在get()上串行执行的,还是通过其他一些黑魔法执行的。 但是,在需要时很容易获得结果:

     auto res = fut.get(); // is an int
  2. 我们现在可以考虑如何以我们控制的方式实现类似async的东西。 例如,我们可能坚持要在单独的线程中执行函数。 我们已经知道我们可以通过std::thread类提供一个单独的线程。

    下一个较低的抽象级别正是这样做的: std::packaged_task 这是一个包装函数并为函数返回值提供未来的模板,但对象本身是可调用的,调用它由用户自行决定。 我们可以这样设置:

     std::packaged_task<int(double, char, bool)> tsk(foo); auto fut = tsk.get_future(); // is a std::future<int>

    一旦我们调用任务并且调用完成,未来就准备好了。 这是单独线程的理想工作。 我们只需要确保将任务移动到线程中:

     std::thread thr(std::move(tsk), 1.5, 'x', false);

    线程立即开始运行。 我们可以detach它,或者在范围的末尾join它,或者在任何时候(例如使用 Anthony Williams 的scoped_thread包装器,它确实应该在标准库中)。 不过,我们在这里不关心使用std::thread的细节; 只要thr最终加入或分离。 重要的是,只要函数调用结束,我们的结果就准备好了:

     auto res = fut.get(); // as before
  3. 现在我们进入最低层:我们将如何实施打包任务? 这就是std::promise的用武之地。promise 是与未来通信的基石。 主要步骤如下:

    • 调用线程作出承诺。

    • 调用线程从承诺中获得未来。

    • promise 和函数参数一起被移到一个单独的线程中。

    • 新线程执行函数并履行承诺。

    • 原始线程检索结果。

    例如,这是我们自己的“打包任务”:

     template <typename> class my_task; template <typename R, typename...Args> class my_task<R(Args...)> { std::function<R(Args...)> fn; std::promise<R> pr; // the promise of the result public: template <typename...Ts> explicit my_task(Ts &&... ts): fn(std::forward<Ts>(ts)...) { } template <typename...Ts> void operator()(Ts &&... ts) { pr.set_value(fn(std::forward<Ts>(ts)...)); // fulfill the promise } std::future<R> get_future() { return pr.get_future(); } // disable copy, default move };

    此模板的用法与std::packaged_task的用法基本相同。 请注意,移动整个任务包含移动承诺。 在更特殊的情况下,还可以将 promise 对象显式移动到新线程中,并使其成为线程函数的函数参数,但像上面这样的任务包装器似乎是一种更灵活、侵入性更小的解决方案。


破例

承诺与异常密切相关。 promise 的接口本身并不足以完全传达其状态,因此只要对 promise 的操作没有意义,就会抛出异常。 所有异常都是std::future_error类型,它派生自std::logic_error 首先,描述一些约束:

  • 默认构造的承诺是不活跃的。 不活跃的承诺可以在没有任何后果的情况下消失。

  • 当通过get_future()获得未来时,承诺就会生效。 但是,可能只能得到一个未来!

  • 如果要消耗其未来,则必须在其生命周期结束之前通过set_value()满足承诺或通过set_exception()设置异常。 一个满足的承诺可以毫无后果地消失,并且get()在未来变得可用。 带有异常的承诺将在将来调用get()时引发存储的异常。 如果 promise 既没有价值也没有异常而死,在未来调用get()将引发“破碎的承诺”异常。

这里有一个小测试系列来演示这些不同的异常行为。 一、线束:

#include <iostream>
#include <future>
#include <exception>
#include <stdexcept>

int test();

int main()
{
    try
    {
        return test();
    }
    catch (std::future_error const & e)
    {
        std::cout << "Future error: " << e.what() << " / " << e.code() << std::endl;
    }
    catch (std::exception const & e)
    {
        std::cout << "Standard exception: " << e.what() << std::endl;
    }
    catch (...)
    {
        std::cout << "Unknown exception." << std::endl;
    }
}

现在开始测试。

案例一:不活跃的承诺

int test()
{
    std::promise<int> pr;
    return 0;
}
// fine, no problems

案例 2:主动承诺,未使用

int test()
{
    std::promise<int> pr;
    auto fut = pr.get_future();
    return 0;
}
// fine, no problems; fut.get() would block indefinitely

案例 3:期货太多

int test()
{
    std::promise<int> pr;
    auto fut1 = pr.get_future();
    auto fut2 = pr.get_future();  //   Error: "Future already retrieved"
    return 0;
}

案例 4:满意的承诺

int test()
{
    std::promise<int> pr;
    auto fut = pr.get_future();

    {
        std::promise<int> pr2(std::move(pr));
        pr2.set_value(10);
    }

    return fut.get();
}
// Fine, returns "10".

案例 5:太过满足

int test()
{
    std::promise<int> pr;
    auto fut = pr.get_future();

    {
        std::promise<int> pr2(std::move(pr));
        pr2.set_value(10);
        pr2.set_value(10);  // Error: "Promise already satisfied"
    }

    return fut.get();
}

如果存在多个set_valueset_exception之一,则会抛出相同的异常。

案例 6:异常

int test()
{
    std::promise<int> pr;
    auto fut = pr.get_future();

    {
        std::promise<int> pr2(std::move(pr));
        pr2.set_exception(std::make_exception_ptr(std::runtime_error("Booboo")));
    }

    return fut.get();
}
// throws the runtime_error exception

案例 7:失信

int test()
{
    std::promise<int> pr;
    auto fut = pr.get_future();

    {
        std::promise<int> pr2(std::move(pr));
    }   // Error: "broken promise"

    return fut.get();
}

用 [futures.state] 的话来说, std::future是一个异步返回对象(“一个从共享状态读取结果的对象”),而std::promise是一个异步提供者(“一个提供结果的对象”到共享状态”),即承诺是你设置结果的东西,这样你就可以从相关的未来得到它。

异步提供程序是最初创建未来引用的共享状态的东西。 std::promise是一种异步提供程序, std::packaged_task是另一种, std::async的内部细节是另一种。 其中每一个都可以创建一个共享状态,并为您提供一个共享该状态的std::future ,并可以使该状态准备就绪。

std::async是一个更高级别的便利实用程序,它为您提供一个异步结果对象,并在内部负责创建异步提供程序并在任务完成时准备好共享状态。 您可以使用std::packaged_task (或std::bindstd::promise )和std::thread来模拟它,但使用std::async更安全、更容易。

std::promise有点低级,因为当你想将异步结果传递给未来,但准备好结果的代码不能包含在适合传递给std::async的单个函数中。 例如,您可能有一个由多个promise和关联的future组成的数组,并且有一个线程执行多个计算并为每个 promise 设置一个结果。 async只允许您返回一个结果,要返回多个结果,您需要多次调用async ,这可能会浪费资源。

Bartosz Milewski提供了一篇很好的文章。

C++ 将 futures 的实现拆分成一组小块

std::promise 是这些部分之一。

promise 是一种工具,用于将执行函数的线程的返回值(或异常)传递给在函数未来兑现的线程。

...

Future 是围绕 promise 通道的接收端构建的同步对象。

所以,如果你想使用未来,你最终会得到一个用于获取异步处理结果的承诺。

该页面的一个示例是:

promise<int> intPromise;
future<int> intFuture = intPromise.get_future();
std::thread t(asyncFun, std::move(intPromise));
// do some other stuff
int result = intFuture.get(); // may throw MyException

在粗略的近似中,您可以将std::promise视为std::future的另一端(这是false ,但为了说明,您可以认为它是)。 通信通道的消费者端将使用std::future来消费来自共享状态的数据,而生产者线程将使用std::promise来写入共享状态。

std::promise是从异步函数返回信息的通道或路径。 std::future是同步机制,它让调用者等待,直到std::promise中携带的返回值准备好(意味着它的值在函数内部设置)。

异步处理中真的有3个核心实体。 C++11 目前专注于其中的两个。

异步运行某些逻辑所需的核心内容是:

  1. 将在“某处”运行的任务(逻辑打包为一些仿函数对象)。
  2. 实际的处理节点——一个线程、一个进程等,当它们被提供给它时运行这些仿函数。 查看“命令”设计模式,了解基本工作线程池如何执行此操作。
  3. 结果句柄:有人需要那个结果,并且需要一个可以为他们获取它的对象。 出于 OOP 和其他原因,任何等待或同步都应在此句柄的 API 中完成。

C++11 调用了我在 (1) std::promise和 (3) std::future中所说的内容。 std::thread是为 (2) 公开提供的唯一内容。 这是不幸的,因为真正的程序需要管理线程和内存资源,并且大多数人希望任务在线程池上运行,而不是为每个小任务创建和销毁线程(这几乎总是会导致不必要的性能损失,并且可以轻松创建资源饥饿更糟)。

根据 Herb Sutter 和 C++11 智囊团中的其他人的说法,有添加一个std::executor的暂定计划——很像在 Java 中——将成为线程池的基础和逻辑上与 (2) 类似的设置。 也许我们会在 C++2014 中看到它,但我的赌注更像是 C++17(如果他们搞砸了这些标准,上帝会帮助我们)。

std::promise被创建为 promise/future 对的端点, std::future (使用get_future()方法从 std::promise 创建)是另一个端点。 这是一种简单的一次性方法,它为两个线程提供了一种同步方式,因为一个线程通过消息向另一个线程提供数据。

您可以将其视为一个线程创建提供数据的承诺,而另一个线程在未来收集承诺。 该机制只能使用一次。

promise/future 机制只有一个方向,从使用std::promiseset_value()方法的线程到使用std::futureget()方法接收数据的线程。 如果多次调用 future 的get()方法,则会生成异常。

如果带有std::promise的线程没有使用set_value()来实现它的承诺,那么当第二个线程调用std::futureget()来收集承诺时,第二个线程将进入等待状态,直到当第一个线程使用set_value()方法发送数据时,promise 由带有std::promise的第一个线程实现。

借助技术规范 N4663 编程语言 — 协程的 C++ 扩展和 Visual Studio 2017 C++ 编译器对co_await的支持所提议的协程,还可以使用std::futurestd::async来编写协程功能。 请参阅https://stackoverflow.com/a/50753040/1466970中的讨论和示例,其中有一个部分讨论了std::futureco_await的使用。

以下示例代码是一个简单的 Visual Studio 2013 Windows 控制台应用程序,展示了使用一些 C++11 并发类/模板和其他功能。 它说明了 promise/future 的使用效果很好,自治线程将执行某些任务并停止,以及需要更多同步行为的使用,并且由于需要多个通知,promise/future 对不起作用。

关于这个例子的一个注意事项是在不同地方添加的延迟。 添加这些延迟只是为了确保使用std::cout打印到控制台的各种消息清晰,并且来自多个线程的文本不会混合在一起。

main()的第一部分是创建三个额外的线程并使用std::promisestd::future在线程之间发送数据。 有趣的一点是主线程启动了一个线程 T2,它将等待来自主线程的数据,做一些事情,然后将数据发送给第三个线程 T3,T3 然后做一些事情并将数据发送回主线程。

main()的第二部分创建两个线程和一组队列,以允许从主线程向两个创建的线程中的每一个发送多条消息。 我们不能为此使用std::promisestd::future ,因为 promise/future duo 是一次性的,不能重复使用。

Sync_queue类的源代码来自 Stroustrup 的 The C++ Programming Language: 4th Edition。

// cpp_threads.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <thread>  // std::thread is defined here
#include <future>  // std::future and std::promise defined here

#include <list>    // std::list which we use to build a message queue on.

static std::atomic<int> kount(1);       // this variable is used to provide an identifier for each thread started.

//------------------------------------------------
// create a simple queue to let us send notifications to some of our threads.
// a future and promise are one shot type of notifications.
// we use Sync_queue<> to have a queue between a producer thread and a consumer thread.
// this code taken from chapter 42 section 42.3.4
//   The C++ Programming Language, 4th Edition by Bjarne Stroustrup
//   copyright 2014 by Pearson Education, Inc.
template<typename Ttype>
class Sync_queue {
public:
    void  put(const Ttype &val);
    void  get(Ttype &val);

private:
    std::mutex mtx;                   // mutex used to synchronize queue access
    std::condition_variable cond;     // used for notifications when things are added to queue
    std::list <Ttype> q;              // list that is used as a message queue
};

template<typename Ttype>
void Sync_queue<Ttype>::put(const Ttype &val) {
    std::lock_guard <std::mutex> lck(mtx);
    q.push_back(val);
    cond.notify_one();
}

template<typename Ttype>
void Sync_queue<Ttype>::get(Ttype &val) {
    std::unique_lock<std::mutex> lck(mtx);
    cond.wait(lck, [this]{return  !q.empty(); });
    val = q.front();
    q.pop_front();
}
//------------------------------------------------


// thread function that starts up and gets its identifier and then
// waits for a promise to be filled by some other thread.
void func(std::promise<int> &jj) {
    int myId = std::atomic_fetch_add(&kount, 1);   // get my identifier
    std::future<int> intFuture(jj.get_future());
    auto ll = intFuture.get();   // wait for the promise attached to the future
    std::cout << "  func " << myId << " future " << ll << std::endl;
}

// function takes a promise from one thread and creates a value to provide as a promise to another thread.
void func2(std::promise<int> &jj, std::promise<int>&pp) {
    int myId = std::atomic_fetch_add(&kount, 1);   // get my identifier
    std::future<int> intFuture(jj.get_future());
    auto ll = intFuture.get();     // wait for the promise attached to the future

    auto promiseValue = ll * 100;   // create the value to provide as promised to the next thread in the chain
    pp.set_value(promiseValue);
    std::cout << "  func2 " << myId << " promised " << promiseValue << " ll was " << ll << std::endl;
}

// thread function that starts up and waits for a series of notifications for work to do.
void func3(Sync_queue<int> &q, int iBegin, int iEnd, int *pInts) {
    int myId = std::atomic_fetch_add(&kount, 1);

    int ll;
    q.get(ll);    // wait on a notification and when we get it, processes it.
    while (ll > 0) {
        std::cout << "  func3 " << myId << " start loop base " << ll << " " << iBegin << " to " << iEnd << std::endl;
        for (int i = iBegin; i < iEnd; i++) {
            pInts[i] = ll + i;
        }
        q.get(ll);  // we finished this job so now wait for the next one.
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::chrono::milliseconds myDur(1000);

    // create our various promise and future objects which we are going to use to synchronise our threads
    // create our three threads which are going to do some simple things.
    std::cout << "MAIN #1 - create our threads." << std::endl;

    // thread T1 is going to wait on a promised int
    std::promise<int> intPromiseT1;
    std::thread t1(func, std::ref(intPromiseT1));

    // thread T2 is going to wait on a promised int and then provide a promised int to thread T3
    std::promise<int> intPromiseT2;
    std::promise<int> intPromiseT3;

    std::thread t2(func2, std::ref(intPromiseT2), std::ref(intPromiseT3));

    // thread T3 is going to wait on a promised int and then provide a promised int to thread Main
    std::promise<int> intPromiseMain;
    std::thread t3(func2, std::ref(intPromiseT3), std::ref(intPromiseMain));

    std::this_thread::sleep_for(myDur);
    std::cout << "MAIN #2 - provide the value for promise #1" << std::endl;
    intPromiseT1.set_value(22);

    std::this_thread::sleep_for(myDur);
    std::cout << "MAIN #2.2 - provide the value for promise #2" << std::endl;
    std::this_thread::sleep_for(myDur);
    intPromiseT2.set_value(1001);
    std::this_thread::sleep_for(myDur);
    std::cout << "MAIN #2.4 - set_value 1001 completed." << std::endl;

    std::future<int> intFutureMain(intPromiseMain.get_future());
    auto t3Promised = intFutureMain.get();
    std::cout << "MAIN #2.3 - intFutureMain.get() from T3. " << t3Promised << std::endl;

    t1.join();
    t2.join();
    t3.join();

    int iArray[100];

    Sync_queue<int> q1;    // notification queue for messages to thread t11
    Sync_queue<int> q2;    // notification queue for messages to thread t12

    std::thread t11(func3, std::ref(q1), 0, 5, iArray);     // start thread t11 with its queue and section of the array
    std::this_thread::sleep_for(myDur);
    std::thread t12(func3, std::ref(q2), 10, 15, iArray);   // start thread t12 with its queue and section of the array
    std::this_thread::sleep_for(myDur);

    // send a series of jobs to our threads by sending notification to each thread's queue.
    for (int i = 0; i < 5; i++) {
        std::cout << "MAIN #11 Loop to do array " << i << std::endl;
        std::this_thread::sleep_for(myDur);  // sleep a moment for I/O to complete
        q1.put(i + 100);
        std::this_thread::sleep_for(myDur);  // sleep a moment for I/O to complete
        q2.put(i + 1000);
        std::this_thread::sleep_for(myDur);  // sleep a moment for I/O to complete
    }

    // close down the job threads so that we can quit.
    q1.put(-1);    // indicate we are done with agreed upon out of range data value
    q2.put(-1);    // indicate we are done with agreed upon out of range data value

    t11.join();
    t12.join();
    return 0;
}

这个简单的应用程序创建以下输出。

MAIN #1 - create our threads.
MAIN #2 - provide the value for promise #1
  func 1 future 22
MAIN #2.2 - provide the value for promise #2
  func2 2 promised 100100 ll was 1001
  func2 3 promised 10010000 ll was 100100
MAIN #2.4 - set_value 1001 completed.
MAIN #2.3 - intFutureMain.get() from T3. 10010000
MAIN #11 Loop to do array 0
  func3 4 start loop base 100 0 to 5
  func3 5 start loop base 1000 10 to 15
MAIN #11 Loop to do array 1
  func3 4 start loop base 101 0 to 5
  func3 5 start loop base 1001 10 to 15
MAIN #11 Loop to do array 2
  func3 4 start loop base 102 0 to 5
  func3 5 start loop base 1002 10 to 15
MAIN #11 Loop to do array 3
  func3 4 start loop base 103 0 to 5
  func3 5 start loop base 1003 10 to 15
MAIN #11 Loop to do array 4
  func3 4 start loop base 104 0 to 5
  func3 5 start loop base 1004 10 to 15

承诺是电线的另一端。

想象一下,您需要检索由async计算的future值。 但是,您不希望它在同一个线程中计算,并且您甚至不“现在”生成一个线程——也许您的软件被设计为从池中选择一个线程,所以您不知道谁会最后执行che计算。

现在,您将什么传递给这个(尚未知的)线程/类/实体? 你不会传递future ,因为这就是结果 你想传递future相关的东西,代表电线的另一端,所以你只会查询future ,而不知道谁会实际计算/写东西。

这是promise 它是连接你future把手 如果future是一个扬声器,并且使用get()你开始收听直到有声音发出,那么promise就是一个麦克风 但不仅仅是任何麦克风,它是用一根电线连接到您手持的扬声器麦克风。 你可能知道谁在另一端,但你不需要知道——你只需给出它并等到对方说些什么。

http://www.cplusplus.com/reference/future/promise/

一句话解释:furture::get()永远等待promse::set_value()。

void print_int(std::future<int>& fut) {
    int x = fut.get(); // future would wait prom.set_value forever
    std::cout << "value: " << x << '\n';
}

int main()
{
    std::promise<int> prom;                      // create promise

    std::future<int> fut = prom.get_future();    // engagement with future

    std::thread th1(print_int, std::ref(fut));  // send future to new thread

    prom.set_value(10);                         // fulfill promise
                                                 // (synchronizes with getting the future)
    th1.join();
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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