What are the benefits of clojure promises using extra hours?

I am considering different ways to implement concurrency in clojure, and these seem to be two competing ways to do the same, so I was wondering where I should use each technique.

+7
source share
2 answers

Watches and promises are both very useful for concurrency, but suitable for slightly different purposes. You may well find that you want to use both in different places in one application.

Use the clock if you want a notification of a link change . For example, if one thread processes events and updates ref in response to some of these events, you can use add-watch so that other parts of your system can receive update notifications. One watch can handle many updates over time.

Use the promise if you want to pass a handle to another thread to access the value that has not yet been calculated . If another thread tries to dereference a promise, they will be blocked until the calculation of the promise is complete (that is, the Source thread places the value in the promise through "deliver"). The only promise is intended to be used only once - after that it is just a fixed value.

+5
source

A clock is one entity in a parallel system and promises are two objects.

promises are more a way to communicate between events on different timelines. They provide a way for a piece of code to receive a response without worrying about which mechanism will provide the response. the source code path can create a promise and pass it to two different code paths in the same thread or threads or agents or nodes in a distributed system. then when one of the threads / agents / refs needs an answer, it can block the promise without having to know anything about the entity that will fulfill the promise. And when another thread / agent / ref / other evaluates the response, it can fulfill the promise without having to know anything about the entity that is waiting for the promise (or not yet waiting).

promises are timing mechanisms that are independent of the mechanism used simultaneously.

A clock is a way of indicating the function of a call when an atom or reflex changes. this is a way of conveying intentions to all future states of a single agent / ref , saying, "Hey, make sure this condition is always true" or "register the change here."

+10
source

All Articles