Capture context surrounding delegate

Nick Treleaven nick at geany.org
Sat May 10 11:34:36 UTC 2025


On Saturday, 10 May 2025 at 04:52:15 UTC, Python wrote:
>     for (int i = 0; i < 100; ++i)
>     {
>         auto captured = i;
>         //how do i capture i?
>         //it's always 99 (or near when first threads are 
> spawned before
>         //for cycle ends)
>         ThreadPool.enqueue(() {
>             writefln("Item: %d on thread %d, running %d 
> threads", captured, Thread.getThis.id, ThreadPool.threads);
>         });
>     }

This is a known issue with capturing variables scoped in a loop:
https://issues.dlang.org/show_bug.cgi?id=21929#c10


More information about the Digitalmars-d-learn mailing list