What are futures?

后端 未结 7 1190
长发绾君心
长发绾君心 2021-02-13 09:37

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

相关标签:
7条回答
  • 2021-02-13 10:04

    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.

    0 讨论(0)
  • 2021-02-13 10:10

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

    0 讨论(0)
  • 2021-02-13 10:14

    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.

    0 讨论(0)
  • 2021-02-13 10:19

    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); }));
    
    0 讨论(0)
  • 2021-02-13 10:26

    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.

    0 讨论(0)
  • 2021-02-13 10:27

    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.

    0 讨论(0)
提交回复
热议问题