views:

457

answers:

6
+8  Q: 

What are futures?

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

+3  A: 

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.

0124816
+4  A: 

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.

John Millikin
AFAIK, futures do not begin executing until they are actually requested. Futures are often used to build a lazy barrier, e.g. in order to implement an infinite structure. If futures began execution immediately upon creation, they would not be suitable for this.
0124816
@012...: Check the links I posted, they give a good overview of futures.
John Millikin
http://en.wikipedia.org/wiki/Futures_and_promises#Distinction_between_futures_and_promisesSeems to be a more definitive reference. (Hah! Wikipedia!) Summary: Futures started out not being concurrently executed; now some define them as doing so, and have renamed the original concept a Promise.
0124816
+4  A: 

There is a Wikipedia article about futures, in short its a way to use a value that is not yet known. The value can be then 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 Nth_prime then pass it to add
int sum = add(Nth_prime(4), Nth_prime(2));

or you can have the lazy way:

int add(future<int> i, future<int> j) { return i + j; }
// Nth_prime are calculated on demand possibly in a separate thread
int sum = add(make_future(Nth_prime, 4), make_future(Nth_prime, 2));

The implementation of future and make_future is left as exercise to the reader, it probably involves std::bind and caching the value ;o)

Motti
+3  A: 

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

Robert Gould
A: 

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.

Apocalisp
A: 

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.

scope_creep