This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Guile's threads and mutex


NIIBE Yutaka writes:
 > I'll show an example later.

Here is small example.

Type (start) to start producer, type (stop) to stop it.

Producer increment current-product-number at priod of one second.
Consumer waits for product, and displays the product number.

Hope this helps, 
-- 
NIIBE Yutaka

==========================
(define mutex (make-mutex))
(define new-product (make-condition-variable))
(define control (make-condition-variable))
(define stop-flag #t)
(define current-product-number 0)
(define procecced-product-number 0)

(define (start)
  (lock-mutex mutex)
  (set! stop-flag #f)
  (signal-condition-variable control)
  (unlock-mutex mutex))

(define (stop)
  (lock-mutex mutex)
  (set! stop-flag #t)
  (unlock-mutex mutex))

(define (producer)
  (let loop ()
    (begin
      (lock-mutex mutex)
      (if stop-flag
	  (wait-condition-variable control mutex))
      (set! current-product-number (1+ current-product-number))
      (signal-condition-variable new-product)
      (unlock-mutex mutex)
      (select () () () 0 1000000)
      (loop))))

(define (consumer)
  (let loop ()
    (begin
      (lock-mutex mutex)
      (if (< procecced-product-number current-product-number)
	  (begin
	    (set! procecced-product-number (1+ procecced-product-number))
	    (display procecced-product-number)
	    (newline))
	  (wait-condition-variable new-product mutex))
      (unlock-mutex mutex)
      (loop))))

(begin-thread
 (producer))
(begin-thread
 (consumer))

==========================