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] |
>>>>> "P" == Pierpaolo Bernardi <bernardp@cli.di.unipi.it> wrote: P> On 27 Apr 1999, Gary Houston wrote: >> Hmm, folk-law says that continuations can be used to simulate >> threads, but I've never seen it done. P> Once upon a time, Siskind posted an implementation of threads P> built on call/cc on the scheme mailing list. P> P. I guess you mean this one: Subject: Re: Threads and call/cc Newsgroups: comp.lang.scheme Date: Thu, 11 Feb 1999 23:47:39 GMT Here is some sample code that will get you started. Its very minimal (only 18 lines of Scheme for the thread primitives) but it serves to get the basic idea of how to do threads with call/cc. Once you understand the basic idea you can improve on the scheduling algorithm and thread queue data structure. Marc Feeley (define thread-queue '()) (define (add-thread proc) (set! thread-queue (append thread-queue (list proc)))) (define (fork-thread thunk) (add-thread (lambda (dummy) (thunk) (terminate-thread)))) (define (switch-thread) (call-with-current-continuation (lambda (cont) (add-thread cont) (terminate-thread)))) (define (terminate-thread) (if (null? thread-queue) (exit) (let ((cont (car thread-queue))) (set! thread-queue (cdr thread-queue)) (cont 'dummy)))) ; A test of the thread routines: (define (repeat x n) (if (> n 0) (begin (switch-thread) (write x) (repeat x (- n 1))))) (fork-thread (lambda () (repeat 'a 5))) (fork-thread (lambda () (repeat 'b 3))) (terminate-thread) ; Output: abababaa ; To get preemptive multitasking with Gambit-C, remove the call to ; (switch-thread) inside repeat and just before the final call to ; (terminate-thread) add: ; (##add-timer-interrupt-job switch-thread) HTH Roland