mailboxes behaviour (cc: andyjpb) #2 added by sjw on Mon Aug 27 18:30:52 2012

(use mailbox srfi-18)
(use miscmacros thread-utils)

(define (log-mailbox mbx)
(printf "((current-thread: ~S) (mailbox-waiting? ~S) (mailbox-waiting? ~S) (mailbox-count ~S))~N"
           (thread-name (current-thread)) (mailbox-waiting? mbx) (mailbox-waiters mbx) (mailbox-count mbx)))

(let* ((my-box (make-mailbox))
       (proc   (lambda () (fprintf (current-error-port) "Message ~S was received by ~S" (mailbox-receive! my-box) (current-thread))))
       (thread (thread-start! (make-thread proc))))

  ; Ensure thread is running.
  (while (thread-ready? thread) (pp (thread-state thread)) (thread-yield!))

  (log-mailbox my-box)  

  ; Check to see if thread is actually watching the mailbox.
  (if (mailbox-waiting? my-box)
    (begin (mailbox-send! my-box "any data")
           (log-mailbox my-box)
    
           (while (mailbox-waiting? my-box) (thread-yield!))
           (log-mailbox my-box)))

  ; Now listen for messages ourselves
  (proc))