ClojureDocs

Nav

Namespaces

vreset!

clojure.core

Available since 1.7 (source)
  • (vreset! vol newval)
Sets the value of volatile to newval without regard for the
 current value. Returns newval.
1 Example
;; A lightweight (but low level) syncrhonization solution with volatile!
;; Prefer promise/deliver for this (unless you know what you're doing).
;; vars/atoms is also possible but they're heavier
;; (locking and CAS respectively).

(def ready (volatile! false))
(def result (volatile! nil))

(defn start-consumer []
  (future
    (while (not @ready) ; consumer starts spinning
      (Thread/yield))   ; release control for other threads
    (println "Consumer getting result:" @result)))

(defn start-producer []
  (future
    (vreset! result :done) ; change the 1st volatile, no reordering (guaranteed).
    (vreset! ready true))) ; change the ready state. 

(start-consumer)
(start-producer)
;; Consumer getting result: :done
See Also

Creates and returns a Volatile with an initial value of val.

Added by kumarshantanu
0 Notes
No notes for vreset!