Added header comment
Removed issues. The real problem here seemed to be a mutex-unlock without a corresponding lock, which results in undefined behavior according to the docs. Everything seems fine if the lock/unlock are not there or if they both are.
This commit is contained in:
Justin Ethier 2016-01-16 23:47:31 -05:00
parent fef18d014a
commit cc630028f9

View file

@ -1,18 +1,17 @@
; TODO: ;; An example program having multiple producers concurrently writing to a queue
; try having multiple producers writing to a queue, and ;; and a single consumer reading from the queue.
; a consumer reading from it
; an important consideration is how to let the producers write values, since they would be on their stacks!
; ideally do not want the application to have to worry about what value came from what thread...
(import (import
(scheme base) (scheme base)
(scheme read) (scheme read)
(scheme write) (scheme write)
(srfi 18)) (srfi 18))
;; Global queue and a lock to coordinate access to it.
(define *lock* (make-mutex)) ;; Note ->heap is used to ensure any objects accessed by multiple threads are
;; not on a thread's local stack, since those objects can be moved at any time
;; by the thread's minor GC.
(define *queue* (->heap (list))) (define *queue* (->heap (list)))
(define *lock* (make-mutex))
(define (producer) (define (producer)
(let loop ((n 100)) (let loop ((n 100))
@ -30,10 +29,6 @@
(let loop () (let loop ()
;(write (list (null? *queue*) *queue*)) ;(write (list (null? *queue*) *queue*))
(define sleep? #f) (define sleep? #f)
;; issues here:
;; - try compiling this but commenting out the Cyc_mutex_lock
;; code in the C. there is a gc_move bag tag error at runtime
;; also get the same result by using read-char below... WTF?
(mutex-lock! *lock*) (mutex-lock! *lock*)
(cond (cond
((not (null? *queue*)) ((not (null? *queue*))
@ -44,8 +39,7 @@
(set! sleep? #t))) (set! sleep? #t)))
(mutex-unlock! *lock*) (mutex-unlock! *lock*)
(if sleep? (thread-sleep! 1000)) (if sleep? (thread-sleep! 1000))
(loop) (loop)))
))
(thread-start! (make-thread producer)) (thread-start! (make-thread producer))
(thread-start! (make-thread producer)) (thread-start! (make-thread producer))