Returns a new serializer. A serializer represents a set of procedures that make up a region of mutual exclusion , or a critial section. When a thread is execting in the critial section, no other thread may enter the critial section. Other threads attempting to call one of the procedures in the set are suspended until the first thread exits the critial section.
make-serializer returns a new serializer which is a higher order procedure. The serializer is applied to a procedure to incorporate it in critial section. The value returned is a procedure with the same behaviour, but the returned procedure also enforces the critical section.
To illustrate the problem that serializers solve, consider the following code. At the end, count could be -1, 0, 1 or 2, depending on the ordering of the three reads of count and the three writes.
(define count 0) (define (add x y) (+ x y)) (define (incr) (set! count (add count 1))) (define (decr) (set! count (add count -1))) (parallel (incr) (decr) (incr))
At the end, count will be 1:
(define count 0) (define (add x y) (+ x y)) (define ((set! count (add count 1))) (define serialized (make-serializer)) (define incr (serialized (define decr (serialized (lambda () (set! count (add count -11))) (parallel (incr) (decr) (incr))
Note that other threads are only prevented from calling incr and decr, and not prevented from calling add or %incr directly.