-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
lowly coder wrote:
I realixe, per section 13.2,
Each thread has a "mailbox" which is used for inter-thread communication.
however, is there a builtin for global mailboxes (mailboxes taht are just variables; rather than mailboxes associated with threads)?
Thanks
Here is a simple implementation of a mailbox:
(define (make-pipe) ; (display "Making pipe")(newline) (let ((cv (make-condition-variable)) (mutex (make-mutex)) (data (cons '() '()))) (lambda args (case (car args) ((cv) cv) ((mutex) mutex) ((data) data) ((setdata) (set! data (cadr args)))))))
(define (with-lock mutex thunk) (dynamic-wind (lambda ()(mutex-lock! mutex)) thunk (lambda ()(mutex-unlock! mutex))))
(define (write-pipe pipe val) (with-lock (pipe 'mutex) (lambda () (set-cdr! (pipe 'data) (cons val (cdr (pipe 'data)))) (condition-variable-signal! (pipe 'cv)))))
(define (read-pipe pipe) ; (display "Reading pipe")(newline) (let ((front (car (pipe 'data)))) (if (pair? front) (let ((ret (car front))) ;(display "object") (set-car! (pipe 'data) (cdr front)) ret) (let loop () (mutex-lock! (pipe 'mutex)) (let ((back (cdr (pipe 'data)))) (if (pair? back) (begin (pipe 'setdata (cons (reverse back) '())) (mutex-unlock! (pipe 'mutex)) (read-pipe pipe)) (begin (mutex-unlock! (pipe 'mutex) (pipe 'cv)) (loop))))))))
- -- If you cannot read my mind, then listen to what I say.
Marijn Schouten (hkBst), Gentoo Lisp project, Gentoo ML http://www.gentoo.org/proj/en/lisp/, #gentoo-{lisp,ml} on FreeNode