Future (programming)

from Wikipedia, the free encyclopedia

A Future ( Engl. , Future ') or a Promise (Engl., Promise') referred to in the programming a wildcard ( proxy ) for a result that is not yet known, mostly because its calculation is not yet completed.

A future is usually the result of an asynchronous call to a function or method and can be used to access the result as soon as it is available. This type of programming allows a largely transparent parallelization of concurrent processes . The concept of futures was introduced in a 1977 article by Henry G. Baker and Carl Hewitt .

A central idea of ​​programming with futures is that futures can be passed on as arguments to other procedure calls. The evaluation of this call can then begin before the result of the future itself is available. This allows a maximum of parallelism. If the new call is again made asynchronously, this is also called pipelining of the futures. Pipelining can be used in particular in distributed applications to minimize the latency of interprocess communication .

function

Futures are a construct for asynchronous interprocess communication . Conceptually, a future offers a get or join function that blocks until the result is available and then returns it. Depending on the implementation, the waiting time can be limited by means of a timeout or the current status can be queried using additional functions.

If futures are integrated directly into the programming language , then often only an asynchronous assignment operator is defined, for example x @ = expression in Flow Java , an experimental programming language based on Java . This means: Start a process to calculate the expression to the right of the operator and assign a future to the variable x for the result. If the variable x is then accessed, the system waits at this point until the result is available.

Programming languages ​​and program libraries that support futures or promises are CORBA (with Asynchronous Method Invocation (AMI) ), and - from Version 5 - Java using Concurrency Utilities , a class library for concurrency. JavaScript has provided these constructs since ECMAScript 6 (although currently still in a restricted form.) Various libraries are also available for C ++ , the best known of which is Boost . Concurrency and futures are also available in the standard library in standard C ++ 11 . Other programming languages ​​with support for futures and promises are Io , Oz , Scheme , Smalltalk and Scala .

In C # 5.0 and Visual Basic 2013 , futures are used implicitly via async and await. A corresponding future class is defined in the parallel extensions and can therefore also be used in older versions and other programming languages. However, you can implement this yourself if required.

construction

A future is a monad with an associated resolver , which assigns a value to the future. The future can be in one of three states:

  • fulfilled (English: promise kept )
  • broken (English: promise broken )
  • waiting (English: promise pending )

If the value is assigned by the resolver and is therefore known, the future generates an event for which callback functions are registered.

example

The following pseudocode shows the use of futures using the asynchronous assignment operator @ = .

var x @= berechneX();   // Beginne Berechnung von x
var y @= berechneY();   // Beginne Berechnung von y
var z = berechneZ();    // Vollständige Berechnung von z
var ergebnis= x + y + z;  // Benutze x, y und z.
                      // Hier muss dann eventuell auf die Berechnung
                      // von x und y gewartet werden.

This results in the following parallelization:

Main thread X thread Y thread
start calculateX ()    
start calculateY () calculateX ()
calculateZ () calculateY ()
wait for x and y
 
calculate result  

Splitting the calculation over several threads can speed up calculations significantly if several main processors (or processor cores ) are available, or if the individual calculations do not load the main processor because they spend a lot of time waiting for peripheral devices .

literature

  • Henry G. Baker and Carl Hewitt: The Incremental Garbage Collection of Processes. Proceeding of the Symposium on Artificial Intelligence Programming Languages. SIGPLAN Notices August 12, 1977.
  • Henry Lieberman: Thinking About Lots of Things at Once without Getting Confused: Parallelism in Act 1. MIT AI memo 626 May 1981.
  • Henry Lieberman: A Preview of Act 1. MIT AI memo 6 June 25, 1981.

Web links

Individual evidence

  1. Promise. In: MDN. Mozilla Foundation, accessed October 31, 2014 .
  2. ^ Asynchronous programming (Windows Store apps). In: MSDN. Microsoft, accessed February 22, 2014 .
  3. Futures. In: MSDN, Parallel Programming with Microsoft .NET. Microsoft, accessed February 22, 2014 .
  4. ^ Dan Vanderboom: Concurrency & Coordination With Futures in C #. In: Critical Development. July 3, 2008, accessed February 22, 2014 .
  5. Matt Kotsenas at. Al .: C # Promises. In: BitBucket. Retrieved February 23, 2014 .