<div>Hi!</div><div><br></div><div>I'm trying to figure out a parallel implementation of for-each (I'm going to use it for spawning system processes mostly).</div><div>Anyway, the problem is like this: If I use any kind of recursion inside the thread created in the (new-thread) proc, I get this error: </div>
<div><div>*** ERROR IN for-each -- Uncaught exception: #<type-exception #31></div><div>(thread-join! '#<thread #32>)</div></div><div><br></div><div>As soon as I remove the call to (recur) (it would happen also with letrec for instance), it works as expected. I really don't understand how internal recursion in the thread could yield this error.</div>
<div><br></div><div>Thanks for your help</div><div><br></div><div><br></div><div><br></div><div>(define (parallel-for-each f l #!key (max-thread-number 2))</div><div> (let* ((pending-jobs l)</div><div> (jobs-mutex (make-mutex))</div>
<div> (available-jobs? (lambda ()</div><div> (mutex-lock! jobs-mutex)</div><div> (let ((more? (not (null? pending-jobs))))</div><div> (mutex-unlock! jobs-mutex)</div>
<div> more?)))</div><div> (next-element (lambda ()</div><div> (mutex-lock! jobs-mutex)</div><div> (let ((next (car pending-jobs)))</div>
<div> (set! pending-jobs (cdr pending-jobs))</div><div> (mutex-unlock! jobs-mutex)</div><div> next)))</div><div> (results '())</div>
<div> (results-mutex (make-mutex))</div><div> (add-to-results! (lambda (r)</div><div> (mutex-lock! results-mutex)</div><div> (set! results (cons r results))</div>
<div> (mutex-unlock! results-mutex))))</div><div> (let ((new-thread</div><div> (lambda ()</div><div> (thread-start! (make-thread</div><div> (lambda ()</div>
<div> (let recur ()</div><div> (add-to-results! (f (next-element)))</div><div> (if (available-jobs?)</div><div> (recur)</div>
<div> (thread-yield!)))))))))</div><div> (let do-times ((n 0)</div><div> (thread-pool '()))</div><div> (if (< n max-thread-number)</div><div> (do-times (++ n)</div>
<div> (cons (new-thread) thread-pool))</div><div> (for-each thread-join! thread-pool))))</div><div> (reverse results)))</div>