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]

thread question



The following program does not behave as I would expect.  I think
it's a bug in my understanding of threads; could someone explain to me
why the program functions the way it does?

I would have expected that the foreground and background threads would
both get a chance to run, but instead the program always prints:
"foreground".

My thinking was that when the foreground thread blocks on select, the
threading system would realize that the background thread was ready to
run (i.e. had returned from select), and would therefore run the
second thread.

thanks,
-russ

#!/opt/guile/bin/guile -s
!#

(define-module (guile-user))
;;; end-header

(define output
  (let ((output-mutex (make-mutex)))
    (lambda args
      (with-mutex output-mutex
	(for-each (lambda (arg) (write arg) (display " ")) args)
	(newline)
	(flush-all-ports)))))

(define (make-looper message)
  (lambda ()
    (let loop ()
      (select () () () 1)
      (output message)
      (loop))))

(begin-thread (make-looper "background"))
((make-looper "foreground"))

--
"Even if you are on the right track, you'll get run over 
if you just sit there."  
             --Will Rogers (1879-1935)