Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Clojure futures and promises differ?

Both futures and promises block until they have calculated their values, so what is the difference between them?

like image 337
yazz.com Avatar asked Jan 07 '11 07:01

yazz.com


People also ask

What is the difference between future and promise?

A future is a placeholder object for a result that does not yet exist. A promise is a writable, single-assignment container, which completes a future. Promises can complete the future with a result to indicate success, or with an exception to indicate failure.

What is a clojure future?

More specifically, a future is a Clojure macro that takes a set of expressions in order to execute them in another thread. The macro returns a reference in memory to the triggered future.

Are futures asynchronous?

A future (lower case “f”) is an instance of the Future (capitalized “F”) class. A future represents the result of an asynchronous operation, and can have two states: uncompleted or completed.

What are futures and promises C++?

Promises and Futures are used to ferry a single object from one thread to another. A std::promise object is set by the thread which generates the result. A std::future object can be used to retrieve a value, to test to see if a value is available, or to halt execution until the value is available.


1 Answers

Answering in Clojure terms, here are some examples from Sean Devlin's screencast:

(def a-promise (promise)) (deliver a-promise :fred)  (def f (future (some-sexp))) (deref f) 

Note that in the promise you are explicitly delivering a value that you select in a later computation (:fred in this case). The future, on the other hand, is being consumed in the same place that it was created. The some-expr is presumably launched behind the scenes and calculated in tandem (eventually), but if it remains unevaluated by the time it is accessed the thread blocks until it is available.


edited to add

To help further distinguish between a promise and a future, note the following:

promise

  1. You create a promise. That promise object can now be passed to any thread.
  2. You continue with calculations. These can be very complicated calculations involving side-effects, downloading data, user input, database access, other promises -- whatever you like. The code will look very much like your mainline code in any program.
  3. When you're finished, you can deliver the results to that promise object.
  4. Any item that tries to deref your promise before you're finished with your calculation will block until you're done. Once you're done and you've delivered the promise, the promise won't block any longer.

future

  1. You create your future. Part of your future is an expression for calculation.
  2. The future may or may not execute concurrently. It could be assigned a thread, possibly from a pool. It could just wait and do nothing. From your perspective you cannot tell.
  3. At some point you (or another thread) derefs the future. If the calculation has already completed, you get the results of it. If it has not already completed, you block until it has. (Presumably if it hasn't started yet, derefing it means that it starts to execute, but this, too, is not guaranteed.)

While you could make the expression in the future as complicated as the code that follows the creation of a promise, it's doubtful that's desirable. This means that futures are really more suited to quick, background-able calculations while promises are really more suited to large, complicated execution paths. Too, promises seem, in terms of calculations available, a little more flexible and oriented toward the promise creator doing the work and another thread reaping the harvest. Futures are more oriented toward automatically starting a thread (without the ugly and error-prone overhead) and going on with other things until you -- the originating thread -- need the results.

like image 157
JUST MY correct OPINION Avatar answered Sep 21 '22 12:09

JUST MY correct OPINION