Welcome to the CHICKEN Scheme pasting service

mailboxes behaviour (cc: andyjpb) #3 added by sjw on Mon Aug 27 18:40:28 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~N" (mailbox-receive! my-box) (current-thread)) (log-mailbox my-box)))
       (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")
           (thread-resume! thread)
           (log-mailbox my-box)
    
           (while (mailbox-waiting? my-box) (thread-yield!))
           (log-mailbox my-box)))

  ; Now listen for messages ourselves
  (proc))

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Which operator can be represented by an apostrophe?
Visually impaired? Let me spell it for you (wav file) download WAV