Fixup I/O

This commit is contained in:
Justin Ethier 2016-04-12 22:22:24 -04:00
parent 1578127410
commit a05e3af2f3
3 changed files with 17 additions and 9 deletions

View file

@ -6,7 +6,7 @@
(srfi 18))
(define (write-forever val)
(write val)
(display val)
(write-forever val))
(define (make-writer val)

View file

@ -18,24 +18,29 @@
(cond
((> n 0)
(mutex-lock! *lock*)
(write (cons 'a *queue*))
(display (cons 'a *queue*))
(newline)
(set! *queue* (->heap (cons (->heap n) *queue*)))
(mutex-unlock! *lock*)
(loop (- n 1)))
(else
(write "producer thread done")))))
(display "producer thread done")
(newline)))))
(define (consumer)
(let loop ()
;(write (list (null? *queue*) *queue*))
;(display (list (null? *queue*) *queue*))
;(newline)
(define sleep? #f)
(mutex-lock! *lock*)
(cond
((not (null? *queue*))
(write (car *queue*))
(display (car *queue*))
(newline)
(set! *queue* (cdr *queue*)))
(else
(write "consumer sleeping")
(display "consumer sleeping")
(newline)
(set! sleep? #t)))
(mutex-unlock! *lock*)
(if sleep? (thread-sleep! 1000))

View file

@ -11,13 +11,16 @@
(thread-start!
(make-thread
(lambda ()
(write "started thread")
(display "started thread")
(newline)
(thread-sleep! 3000)
(write "thread done")
(display "thread done")
(newline)
(condition-variable-broadcast! cv))))
;; Main thread - wait for thread to broadcast it is done
(mutex-lock! m)
(mutex-unlock! m cv) ;; Wait on cv
(write "main thread done")
(display "main thread done")
(newline)
(thread-sleep! 500)