[plt-scheme] Thread synchronization for dummies

From: Sam TH (samth at ccs.neu.edu)
Date: Thu May 28 16:07:34 EDT 2009

On Thu, May 28, 2009 at 3:35 PM, Carl Eastlund <carl.eastlund at gmail.com> wrote:
> And here you go.  The define/sync function will define synchronous
> functions for you, and there is an example of using it below.  Again,
> there are A and B clients, and their start/end messages should never
> be interleaved.
>
> #lang scheme
>
> ;; Macro for synchronous funcitons
> (define-syntax-rule (define/sync (name . args) . body)
>  (define name
>    (let ([s (make-semaphore 1)])
>      (lambda args
>        (dynamic-wind
>         (lambda () (semaphore-wait s))
>         (lambda () . body)
>         (lambda () (semaphore-post s)))))))

I think this can be simplified (and probably improved) to:

(define name
  (let ([s (make-semaphore 1)])
    (lambda args (call-with-semaphore s (lambda () . body)))))

-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.