launch-process-thread added by megane on Fri May 21 14:05:17 2021

(module
 process-launch () (import scheme)
 (cond-expand
  (chicken-5 (import (chicken base))
             (import srfi-18)
             (import (chicken process))
             (import (chicken format))
             (import (chicken time)))
  (else (import chicken)
        (use posix)
        (use (only srfi-18 make-thread thread-start! thread-yield! thread-join!))))

 (define (launch-process-thread cmd #!optional (cb (lambda _ _)))
   (let ([t (make-thread
             (lambda []
               (let ([pid (process-run cmd)])
                 (let lp ()
                   (receive (pid2 ok? exit-status) (process-wait pid #t)
                     (if (zero? pid2)
                         (begin (thread-yield!)
                                (lp))
                         (cb ok? exit-status)))))))])
     (thread-start! t)
     t))

 (define threads '())
 (define (launch arg)
   (set! threads (cons (launch-process-thread (format "sleep ~a; echo ~a" arg arg)
                                              (lambda ret (print arg " done " ret))) threads)))

 (launch 1)
 (launch 3)
 (time (for-each thread-join! threads))

 )