Re: Inconsistent loop behaviour when using threads.

Robert Smith <[email protected]> Wed, 23 Apr 2025 06:15:38 -0700
Newsgroups gmane.lisp.steel-bank.general
Message-ID <CAPbE8xAnmnqsDRvCD+xj-HMyp8=OwsZFRPTqKEoNSG26dK1FDg@mail.gmail.com>
instead do

(loop for i from 0 to 1000
  do (make-thread (let ((i i)) (lambda ...))))

LOOP may mutate the variable i, and that same variable is being captured by
all of your lambdas. Therefore, bind a fresh lexical variable (of the same
name) so each lambda gets its own binding.

Robert

On Wed, Apr 23, 2025 at 06:13 Lubwama Abdul Muswawir <[email protected]> wrote:

> Hi, I am currently exploring the actor concurrent models in SBCL. The loop
> macro is behaving wierdly when using threads inside it. Example loop:
>
> ```
> (eval-when (:compile-toplevel)
>   (defun test-p ()
>     (loop for i from 0 to 1000
>   do (make-thread (lambda () (format #.*terminal-io* "Working on: ~a~%"
> i))))))
> ```
> This will print from 1 to 1001, it will work within expected bounds when I
> use dolist, when I don't use threads. My use case involves using threads on
> a range which I intended to generate via loop.
>
> This behaviour is consistent with all actor packages (sento, lisp-actors)
> I have tried because all use sb-thread.
> _______________________________________________
> Sbcl-help mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sbcl-help
>

_______________________________________________
Sbcl-help mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sbcl-help