unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* racing srfi-18 threads
@ 2009-11-06  9:29 Tristan Colgate
  2009-11-06 14:52 ` Tristan Colgate
  0 siblings, 1 reply; 9+ messages in thread
From: Tristan Colgate @ 2009-11-06  9:29 UTC (permalink / raw)
  To: guile-user

Hi All,

 I've been experimenting with guile-1.9 (latest git)  srfi-18 support.
I'm trying
to implement a race between threads. I've written a macro that take a
routine and a list of args, starts a thread for each arg and runs the
routine with that argument. The result  returned should be the return
result of the first thread to return. I was originally using some of
guile other thread primitives (monitor in particular), but have
stripped those out in order to try testing against other schemes.

 Chicken currently gives the "correct" result, but that uses
cooperative threading. Guile launches threads, I can see them with a
ps -efL,  but they never seem to start (even after the thread-start!).

 The code is as follows....

(require-extension (srfi 18))

(define-syntax race-each
 (syntax-rules ()
 ((_ func parargs)
  (let* ((result #f)
         (result-ready (make-condition-variable))
         (junktex (make-mutex))
         (junk    (mutex-lock! junktex))
         (resulttex (make-mutex))
         (dotask (lambda(arg)
                    (let ((thisresult (func arg)))
                      (with-exception-handler
                        (lambda(ev)
                          (thread-terminate! (current-thread)))
                        (lambda() (mutex-lock! resulttex)))
                      (set! result thisresult)
                      (condition-variable-signal! result-ready)
                      (thread-terminate! (current-thread)))))
         (threads (map (lambda(x)
                         (thread-start! (make-thread (lambda()
                                                        (dotask x)))))
parargs)))
     (mutex-unlock! junktex result-ready)
     (map (lambda(old-thread) (thread-terminate! old-thread)) threads)
     result))))

(display
 (race-each
   (lambda(value)
     (format #t "In thread~%")
     (thread-sleep! (seconds->time (+ (time->seconds (current-time)) value)))
     value)
   '(3 5 8 9 2)))


-- 
Tristan Colgate-McFarlane
----
  "You can get all your daily vitamins from 52 pints of guiness, and a
glass of milk"




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-12-12 17:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-06  9:29 racing srfi-18 threads Tristan Colgate
2009-11-06 14:52 ` Tristan Colgate
2009-11-08 23:39   ` Neil Jerram
2009-11-16 22:16     ` Neil Jerram
2009-11-17 19:58       ` Andy Wingo
2009-11-20  0:00         ` Neil Jerram
2009-12-02 21:46           ` Neil Jerram
2009-12-10 19:53             ` Andy Wingo
2009-12-12 17:00               ` Debugging infrastructure (was Re: racing srfi-18 threads) Neil Jerram

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).