[fpc-pascal] Function reference doesn't capture in thread
Hairy Pixels
genericptr at gmail.com
Sat Sep 17 17:04:56 CEST 2022
> On Sep 17, 2022, at 9:43 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
>
> Then you didn't read my announcement mail deeply enough, cause it's mentioned there!
Yes I just read that over and I remember it now. I feel like this not the same behavior in other languages although I’d need to test. Most recently I’m using Swift for my day job and I don’t recall ever having confusions like this come up.
> And they are not pointers, but the variable is moved to an object instance which implements the interface of the function reference.
“Moved to an object” makes me think the variable is copied but the reference behavior you describe sounds like a pointer so I’m still not 100% clear on what you mean.
>
>
> Even so I’m trying now to modifying the example by copying to a local variable but I still see this same behavior suggesting there isn’t actually a copy. Why is this?
>
> When your callback is executed i might already have been changed!
Ok that makes sense now since it’s just a reference. So this would have worked if I would have passed in i as a parameter and then stored it locally? I’m thinking now how I can preserve a copy of the state in the scope for each thread and I think local vars are the only way, correct? We need some more thread based examples to help people understand I think because I’m finding all sorts of edge cases updating a little thread library myself.
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list