mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-18 21:29:18 +02:00
Fixup I/O
This commit is contained in:
parent
1578127410
commit
a05e3af2f3
3 changed files with 17 additions and 9 deletions
|
@ -6,7 +6,7 @@
|
||||||
(srfi 18))
|
(srfi 18))
|
||||||
|
|
||||||
(define (write-forever val)
|
(define (write-forever val)
|
||||||
(write val)
|
(display val)
|
||||||
(write-forever val))
|
(write-forever val))
|
||||||
|
|
||||||
(define (make-writer val)
|
(define (make-writer val)
|
||||||
|
|
|
@ -18,24 +18,29 @@
|
||||||
(cond
|
(cond
|
||||||
((> n 0)
|
((> n 0)
|
||||||
(mutex-lock! *lock*)
|
(mutex-lock! *lock*)
|
||||||
(write (cons 'a *queue*))
|
(display (cons 'a *queue*))
|
||||||
|
(newline)
|
||||||
(set! *queue* (->heap (cons (->heap n) *queue*)))
|
(set! *queue* (->heap (cons (->heap n) *queue*)))
|
||||||
(mutex-unlock! *lock*)
|
(mutex-unlock! *lock*)
|
||||||
(loop (- n 1)))
|
(loop (- n 1)))
|
||||||
(else
|
(else
|
||||||
(write "producer thread done")))))
|
(display "producer thread done")
|
||||||
|
(newline)))))
|
||||||
|
|
||||||
(define (consumer)
|
(define (consumer)
|
||||||
(let loop ()
|
(let loop ()
|
||||||
;(write (list (null? *queue*) *queue*))
|
;(display (list (null? *queue*) *queue*))
|
||||||
|
;(newline)
|
||||||
(define sleep? #f)
|
(define sleep? #f)
|
||||||
(mutex-lock! *lock*)
|
(mutex-lock! *lock*)
|
||||||
(cond
|
(cond
|
||||||
((not (null? *queue*))
|
((not (null? *queue*))
|
||||||
(write (car *queue*))
|
(display (car *queue*))
|
||||||
|
(newline)
|
||||||
(set! *queue* (cdr *queue*)))
|
(set! *queue* (cdr *queue*)))
|
||||||
(else
|
(else
|
||||||
(write "consumer sleeping")
|
(display "consumer sleeping")
|
||||||
|
(newline)
|
||||||
(set! sleep? #t)))
|
(set! sleep? #t)))
|
||||||
(mutex-unlock! *lock*)
|
(mutex-unlock! *lock*)
|
||||||
(if sleep? (thread-sleep! 1000))
|
(if sleep? (thread-sleep! 1000))
|
||||||
|
|
|
@ -11,13 +11,16 @@
|
||||||
(thread-start!
|
(thread-start!
|
||||||
(make-thread
|
(make-thread
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(write "started thread")
|
(display "started thread")
|
||||||
|
(newline)
|
||||||
(thread-sleep! 3000)
|
(thread-sleep! 3000)
|
||||||
(write "thread done")
|
(display "thread done")
|
||||||
|
(newline)
|
||||||
(condition-variable-broadcast! cv))))
|
(condition-variable-broadcast! cv))))
|
||||||
|
|
||||||
;; Main thread - wait for thread to broadcast it is done
|
;; Main thread - wait for thread to broadcast it is done
|
||||||
(mutex-lock! m)
|
(mutex-lock! m)
|
||||||
(mutex-unlock! m cv) ;; Wait on cv
|
(mutex-unlock! m cv) ;; Wait on cv
|
||||||
(write "main thread done")
|
(display "main thread done")
|
||||||
|
(newline)
|
||||||
(thread-sleep! 500)
|
(thread-sleep! 500)
|
||||||
|
|
Loading…
Add table
Reference in a new issue