tags:

views:

167

answers:

1

The Clojure API describes these two functions as:

(send a f & args) - Dispatch an action to an agent. Returns the agent immediately. Subsequently, in a thread from a thread pool, the state of the agent will be set to the value of: (apply action-fn state-of-agent args)

and

(send-off a f & args) - Dispatch a potentially blocking action to an agent. Returns the agent immediately. Subsequently, in a separate thread, the state of the agent will be set to the value of: (apply action-fn state-of-agent args)

The only obvious difference is send-off should be used when an action may block. Can somebody explain this difference in functionality in greater detail?

+4  A: 

all the actions that get sent to any agent using send are run in a threadpool with a couple more threads than the physical number of processors. this causes them to run closer to the cpu's full capacity. if you make 1000 calls using send you don't really incur much switching overhead, the calls that cant be processed immediately just wait until a processor becomes available. If they block then the thread pool can run dry

when you use send-off a new thread is created for each call. if you send-off 1000 functions the ones that cant be processed immediately still wait for the next available processor and they may incur the extra overhead of starting a thread if the "send-off threadpool" happens to be running low . its ok if the threads block because each task (potentially) gets a dedicated thread.

Arthur Ulfeldt
send-off does not really create a new thread, it uses a different, expandable thread pool.
pmf
thanks: I'll edit to include that. thanks for pointing that out.
Arthur Ulfeldt