SICP Concurrency Language

SICP Concurrency Language

Danny Yoo ( /

Index terms: _sicp_ _concurrency_ _sicp-concurrency_


This provides primitives for doing concurrency, meant to be used with
material from The Structure and Interpretation of Computer Programs
(SICP) Chapter 3.4.

After this PLaneT module is locally installed and DrScheme is
restarted, a new language level called SICP Concurrency should be
selectable.  Alternatively, the provided module ""
can be used as a module language.

Example usage

    (module test-concurrency
      (planet "" ("dyoo" "sicp-concurrency.plt"))
      (define (test-1)
        (define x 10)
        (parallel-execute (lambda () (set! x (* x x)))
                          (lambda () (set! x (+ x 1))))

      (define (test-2)
        (define x 10)
        (define s (make-serializer))
        (parallel-execute (s (lambda () (set! x (* x x))))
                          (s (lambda () (set! x (+ x 1)))))

This example comes from SICP Section 3.4.2 (Mechanisms for Controlling
Concurrency).  If we run TEST-1 for multiple rounds, we'll see several
different and surprising values depending on the interleaving that
happens with PARALLEL-EXECUTE.  TEST-2, on the other hand uses a
serializer to protect the respective critical regions.

API for (planet "" ("dyoo" "sicp-concurrency.plt"))

This module language provides everything that mzscheme provides,
except for:


which are replaced with custom version of these to cause more
interesting concurrency interactions.

We extend the language with following primitives:

> parallel-execute: thunks* -> void

Evaluates any number of thunks in parallel.  The scheduler runs in
round-robin order.  There are no guarantees on how context is switched
between the evaluated thunks.

> make-serializer: -> (thunk -> void)

Constructs a serializer that consumes a thunk and evaluates it

> test-and-set!: (cons boolean X) -> boolean

Given a "cell" whose CAR is a boolean, atomically tests the cell.  As
in SICP, it evaluates to whatever the content of the cell is, but if
it was originally #f, sets it to #t.


Thanks to Jens Axel Sooegard, for pretty much writing the hard stuff
in this module.  Most of this is taken from his work; I just added a
nice language package for this.  Also thanks to the PLT team for a
nice programming environment.