Re: Weird behaviour of SRFI-127 lseq in a multithreaded program
Kon Lovett via Chicken-users <[email protected]> Sun, 22 Feb 2026 11:20:38 -0800
| Newsgroups | gmane.lisp.scheme.chicken |
|---|---|
| Message-ID | <[email protected]> |
;; Eagerly apply a proc to the elements of lseqs ;; Included because it's a common operation, even though it is trivial (define (lseq-for-each proc . lseqs) (apply for-each proc (map lseq-realize lseqs))) lseq-for-each* applies the proc before processing the remaining list, not the egg source. > On Feb 22, 2026, at 10:22 AM, Stanislav Kljuhhin via Chicken-users <[email protected]> wrote: > > Hello, > > Consider the following minimal example: > > ---8<--- > (import srfi-18) > (import srfi-121) > (import srfi-127) > > (define (emit yield) > (let loop ((i 1)) > (thread-sleep! 1) > (yield "hello") > (when (< i 10) > (loop (add1 i))))) > > (define (collect s) > (display s) > (newline)) > > (define (lseq-for-each* proc lseq) > (unless (null? lseq) > (proc (lseq-car lseq)) > (lseq-for-each* proc (lseq-cdr lseq)))) > > (lseq-for-each collect (generator->lseq (make-coroutine-generator emit))) > ---8<—-- > > When I run this program (compiled or interpreted, makes no difference), nothing > happens for 10 seconds, then 10 hello's are printed at once. But if I use the > in-place implementation lseq-for-each*, then the program behaves as one might > expect, i.e. hello's are printed with a 1-second interval. > > Here I used a simplified implementation of lseq-for-each*, just for brevity, but > we can copy the SRFI-127 implementation verbatim, and it still works the same. > > Can you help me understand what is going on, please? > > — > Stan