What are futures?

风流意气都作罢 提交于 2019-12-12 07:47:00

问题


What are futures? It's something to do with lazy evaluation.


回答1:


There is a Wikipedia article about futures. In short, it's a way to use a value that is not yet known. The value can then be calculated on demand (lazy evaluation) and, optionally, concurrently with the main calculation.

C++ example follows.


Say you want to calculate the sum of two numbers. You can either have the typical eager implementation:

int add(int i, int j) { return i + j; }
// first calculate both Nth_prime results then pass them to add
int sum = add(Nth_prime(4), Nth_prime(2)); 

or you can use the futures way using C++11's std::async, which returns an std::future. In this case, the add function will only block if it tries to use a value that hasn't yet been computed (one can also create a purely lazy alternative).

int add(future<int> i, future<int> j) { return i.get() + j.get(); }
int sum = add(async(launch::async, [](){ return Nth_prime(4); }),
              async(launch::async, [](){ return Nth_prime(2); }));



回答2:


When you create a future, a new background thread is started that begins calculating the real value. If you request the value of the future, it will block until the thread has finished calculating. This is very useful for when you need to generate some values in parallel and don't want to manually keep track of it all.

See lazy.rb for Ruby, or Scala, futures, and lazy evaluation.

They can probably be implemented in any language with threads, though it would obviously be more difficult in a low-level language like C than in a high-level functional language.




回答3:


Everyone mentions futures for the purpose of lazy calculation. However another use that isn't as advertised is the use of Futures for IO in general. Especially they're useful for loading files and waiting on network data




回答4:


A Future encapsulates a deferred calculation, and is commonly used to shoehorn lazy evaluation into a non-lazy language. The first time a future is evaluated, the code required to evaluate it is run, and the future is replaced with the result.

Since the future is replaced, subsequent evaluations do not execute the code again, and simply yield the result.




回答5:


The Wiki Article gives a good overview of Futures. The concept is generally used in concurrent systems, for scheduling computations over values that may or may not have been computed yet, and further, whose computation may or may not already be in progress.

From the article:

A future is associated with a specific thread that computes its value. This computation may be started either eagerly when the future is created, or lazily when its value is first needed.

Not mentioned in the article, futures are a Monad, and so it is possible to project functions on future values into the monad to have them applied to the future value when it becomes available, yielding another future which in turn represents the result of that function.




回答6:


Futures are also used in certain design patterns, particularly for real time patterns, for example, the ActiveObject pattern, which seperates method invocation from method execution. The future is setup to wait for the completed execution. I tend to see it when you need to move from a multithreaded enviroment to communicate with a single threaded environment. There may be instances where a piece of hardware doesn't have kernel support for threading, and futures are used in this instance. At first glance it not obvious how you would communicate, and surprisingly futures make it fairly simple. I've got a bit of c# code. I'll dig it out and post it.




回答7:


This blog post gives a very thorough explanation together with an example of how you could implement a future yourself. I really recommend it :)



来源:https://stackoverflow.com/questions/80447/what-are-futures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!