Welcome to the CHICKEN Scheme pasting service

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

 process-launch () (import scheme)
  (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!)
                         (cb ok? exit-status)))))))])
     (thread-start! 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))


Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
What's the result of `(and #t (or 'damn 'you 'spammers))'?
Visually impaired? Let me spell it for you (wav file) download WAV