-1

在很多地方,我发现类似:

std::move(A).thenValue(B)

thenValue() 是否阻塞了未来的“A”。我通读了 Folly 文档,但无法理解。 https://github.com/facebook/folly/blob/master/folly/docs/Futures.md

4

1 回答 1

1

不,它没有阻塞。从概念上讲,您可以这样想签名(尽管这不是实际的签名):

template <typename T>
template <typename TResult>
Future<TResult> Future<T>::thenValue(std::function<TResult(T&&)> callback) { ... }

基本思想是,如果代表的futurestd::move(A)成功,则执行传入的callback( B),传入Afuture产生的值。回调的返回值成为由thenValue().

如果你是一个视觉型的人(比如我),那么标记部分签名可能会有所帮助:

template <typename T>
template <typename TResult>
Future<TResult> Future<T>::thenValue(std::function<TResult(T&&)> callback) { ... }
^^^^^^^^^^^^^^^ ^^^^^^^^^                                        ^^^^^^^^
       3            1                                                2
  1. 您正在调用的未来thenValue
  2. 您传递给的回调函数thenValue
  3. 未来归来thenValue

当 (1) 有一个成功的结果时, (2) 会使用该结果调用。当(2)返回时,(3)产生(2)的返回值。

future (3) 是同步构造的,但回调 (2) 是异步调用的。

于 2020-05-29T01:42:55.740 回答