On this page:
box-cas-update!
box-cas-update!*
box-add1!
box-sub1!

2.2 Boxes🔗

 (require toolbox/box) package: toolbox-lib

procedure

(box-cas-update! box proc)  any/c

  box : (and/c box? (not/c immutable?) (not/c impersonator?))
  proc : (-> any/c any/c)
Atomically updates the contents of box by applying proc to the old value to produce a new value. The proc procedure will be applied more than once if the box is concurrently modified between reading the old value and writing the new one, so proc should generally be inexpensive. The result of the call to box-cas-update! is the value written to box.

Examples:
> (define b (box "hello"))
> (box-cas-update! b string-upcase)

"HELLO"

> (unbox b)

"HELLO"

procedure

(box-cas-update!* box proc)  any/c

  box : (and/c box? (not/c immutable?) (not/c impersonator?))
  proc : (-> any/c (values any/c any/c))
Like box-cas-update!, but proc should return two values: the first value is returned, and the second value is written to box.

Examples:
> (define b (box "old"))
> (box-cas-update!* b (λ (old) (values old "new")))

"old"

> (unbox b)

"new"

procedure

(box-add1! box)  number?

  box : (and/c box? (not/c immutable?) (not/c impersonator?))
Equivalent to (box-cas-update! box add1).

procedure

(box-sub1! box)  number?

  box : (and/c box? (not/c immutable?) (not/c impersonator?))
Equivalent to (box-cas-update! box sub1).