(define (award) - PowerPoint PPT Presentation

About This Presentation
Title:

(define (award)

Description:

And the Oscar goes to Harrison Ford. Meryl Streep. And the Oscar goes to Meryl Streep ... The envelope please...Gwyneth Paltrow. And the Oscar goes to Gwyneth Paltrow ... – PowerPoint PPT presentation

Number of Views:100
Avg rating:3.0/5.0
Slides: 12
Provided by: dexter8
Category:

less

Transcript and Presenter's Notes

Title: (define (award)


1
(define (award) (let ((x (read-line)))
(when (equal? x "bye") (printf "Good
night!n") (kill-thread (current-thread)))
(printf "And the Oscar goes to an" x))
(award)) (thread award) gt Harrison Ford And
the Oscar goes to Harrison Ford Meryl Streep And
the Oscar goes to Meryl Streep bye Good night!
2
(define (wait-for-input) (unless
(char-ready?) (printf "The envelope
please...n") (sleep 4)
(wait-for-input))) (define (award)
(wait-for-input) (let ((x (read-line)))
(when (equal? x "bye") (printf "Good
night!n") (kill-thread (current-thread)))
(printf "And the Oscar goes to an" x))
(award)) (thread award)
3
gt The envelope please...The envelope
please...Mel Gibson And the Oscar goes to Mel
GibsonThe envelope please...The envelope
please...The envelope please...Gwyneth Paltrow
And the Oscar goes to Gwyneth PaltrowThe
envelope please...bye Good night!
4
(define (random-timeout) ( (current-seconds)
( (random 5) 3))) (define (make-dumb-thread n)
(let ((stop-time ( (current-seconds) 30))
(timeout (random-timeout))) (letrec ((f
(lambda () (cond ((gt (current-seconds)
stop-time) (printf "na
terminated " n) (kill-thread
(current-thread))) ((gt
(current-seconds) timeout) (printf
"a " n) (set! timeout
(random-timeout)))) (f))))
f))) (thread (make-dumb-thread 0)) (thread
(make-dumb-thread 1)) (thread (make-dumb-thread
2))
5
gt 1 0 2 2 1 0 2 1 0 2 0 1 2 terminated 1
terminated 0 terminated
6
(define (make-echoer name inport outport)  (let
((stop-time ( (current-seconds) 60))       
(timeout (random-timeout)))    (letrec ((f
(lambda ()      (cond ((gt (current-seconds)
stop-time)             (printf "a terminatedn"
name)             (kill-thread
(current-thread)))            ((char-ready?
inport)             (let ((input (read-line
inport)))               (printf "a received
an" name input)))            ((gt
(current-seconds) timeout)             (let
((msg (next-message)))               (printf
"a sending an" name msg)              
(display msg outport)               (newline
outport)             (set! timeout
(random-timeout)))))      (f))))    f))) 
7
(define next-message  (let ((message-mutex
(make-semaphore 1))        (message-number
0))    (lambda ()      (semaphore-wait
message-mutex)      (let ((m message-number))   
     (set! message-number ( message-number
1))        (semaphore-post message-mutex)       
m))))
8
(let-values (((i1 o1) (make-pipe))            
((i2 o2) (make-pipe)))  (thread (make-echoer
"alice" i1 o2))  (thread (make-echoer "bob" i2
o1))))
9
gt alice sending 0 bob received 0 bob sending
1 alice received 1 alice sending 2 bob
received 2 bob sending 3 alice received 3 bob
sending 4 alice received 4
bob sending 5 alice received 5 alice sending
6 bob received 6 bob sending 7 alice received
7 alice sending 8 bob received 8 bob
terminated alice terminated
10
(define listener (tcp-listen 666))(let-values
(((client-in client-out)
(tcp-connect "24.24.17.114" 666))            
((server-in server-out) (tcp-accept
listener)))  (thread (make-echoer "alice"
client-in client-out))  (thread (make-echoer
"bob"   server-in server-out)))
11
gtbob sending 0alice received 0alice
sending 1bob received 1alice sending 2bob
received 2bob sending 3alice received
3alice sending 4bob received 4
bob sending 5alice received 5alice sending
6bob received 6bob sending 7alice received
7alice sending 8bob received 8alice
terminatedbob terminated
Write a Comment
User Comments (0)
About PowerShow.com