Changeset 36211 in project


Ignore:
Timestamp:
08/12/18 02:49:19 (13 months ago)
Author:
Kon Lovett
Message:

fix example

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/5/mailbox

    r36210 r36211  
    199199
    200200<enscript language=scheme>
     201(import (srfi 18) mailbox)
     202
     203(define (say dir msg)
     204  (print (current-thread) ": " dir ": " msg) )
     205
     206(define (producer ch x)
     207  (say "writing" x)
     208  (mailbox-send! ch x) )
     209
    201210(define (consumer ch)
    202211  (make-thread
    203212    (lambda ()
    204213      (let loop ()
    205         (print (current-thread) ": reading " (mailbox-receive! ch))
    206         (loop) ) ) ) )
     214        (let ((x (mailbox-receive! ch)))
     215          (say "reading" x)
     216          (unless (eq? 'quit x) (loop) ) ) ) ) ) )
    207217
    208218(define ch (make-mailbox))
    209 (thread-start! (consumer ch))
    210 (for-each
    211   (lambda (x)
    212     (print (current-thread) ": writing " x)
    213     (mailbox-send! ch x) )
    214   '(33 44 55 hello) )
     219(define t (thread-start! (consumer ch)))
     220(for-each (cut producer ch <>) '(ready set go quit))
     221(thread-join! t)
    215222</enscript>
    216223
     
    220227* Cannot wait with timeout on the primordial thread. So {{mailbox-wait!}},
    221228{{mailbox-receive!}}, and {{mailbox-cursor-next}} must not be used with a
    222 timeout value when the {{(current-thread)}} is the
    223 {{##sys#primordial-thread}}.
    224 
    225 * A "deadlock" situation is possible when using {{mailbox-wait!}},
     229timeout value when the {{(current-thread)}} is the {{##sys#primordial-thread}}.
     230
     231* A ''deadlock'' is possible when using {{mailbox-wait!}},
    226232{{mailbox-receive!}}, or {{mailbox-cursor-next}} should there be no other
    227 threads "runnable." The problem is when the mailbox is empty the
    228 current-thread is suspended (indefinite block). When the scheduler looks for
    229 the next ready thread and one is not available it signals "deadlock." Using a
    230 {{TIMEOUT}} will allow the calling thread to unblock eventually.
    231 
    232 * A mailbox-cursor must live in an environment where mailbox entries are added
    233 and removed asynchronously. The cursor does '''not''' see a snapshot of the
    234 mailbox. The current state of the mailbox is queried.
     233threads ''runnable''.
     234
     235When a mailbox is empty the {{(current-thread)}} is suspended (indefinite
     236block). Then the scheduler looks for the next ready thread. If one is not
     237available it signals ''deadlock''. Using a {{TIMEOUT}} will allow the calling
     238thread to unblock eventually.
     239
     240* A {{mailbox-cursor}} must live in an environment where {{mailbox}} entries
     241are added and removed asynchronously. The cursor does '''not''' see a snapshot
     242of the mailbox. The current state of the mailbox is queried.
    235243
    236244== Author
Note: See TracChangeset for help on using the changeset viewer.