Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created September 23, 2009 20:26
Show Gist options
  • Save cgrand/192256 to your computer and use it in GitHub Desktop.
Save cgrand/192256 to your computer and use it in GitHub Desktop.
;; philosophers must be initialized to 0
(defn behave [qty id]
(when (pos? @rounds) (send-off *agent* behave id))
(or
(dosync ; Initiate transaction
(when (and
(pos? @rounds) ; Is there more food?
(> 5 (rand-int 10)) ; Do I want to eat or think?
(got-forks? id)) ; Are both of my forks available?
(handle-forks id :take)
(alter rounds dec)
(send logger debug id "ate " @rounds)
(handle-forks id :release)
(inc qty)))
(do
(send logger debug id "thinks " @rounds)
qty)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment