Re: dispatch_apply() on an NSArray and Thread Sanitizer
Re: dispatch_apply() on an NSArray and Thread Sanitizer
- Subject: Re: dispatch_apply() on an NSArray and Thread Sanitizer
- From: Saagar Jha via Cocoa-dev <email@hidden>
- Date: Tue, 19 Apr 2022 15:47:45 -0700
If Thread Sanitizer says your code has a race, it almost certainly has a race.
Your simple code seems OK superficially, but there are a couple things that
could be problematic here: either your real code is actually mutating
something, or (unlikely) you are touching some internal state, perhaps a CoW
optimization, that is not visible to you but is silently changing things under
the hood. In any case, I would generally suggest using -[NSArray
enumerateObjectsAtIndexes:options:usingBlock:] with the NSEnumerationConcurrent
flag, which should rule out any issues with concurrent access on the array
itself.
Regards,
Saagar Jha
> On Apr 19, 2022, at 14:02, Sean McBride via Cocoa-dev
> <email@hidden> wrote:
>
> Hi all,
>
> If one wants to do something with every item in an NSArray in a concurrent
> way, is the following safe/correct?
>
>
> NSArray* array = @[@5, @6, @7, @8, @9, @10, @11];
>
> dispatch_apply([array count], DISPATCH_APPLY_AUTO, ^(size_t idx) {
> id unused = array[idx];
> });
>
>
> Here of course I'm not doing anything interesting, just extracting the object
> from the array. This is a reduced example.
>
> On one Mac I have, Thread Sanitizer says there is a data race there. On
> another Mac, Thread Sanitizer does not complain.
>
> My understanding is that this code is correct, and that TSan is wrong to
> complain, but maybe I'm wrong...
>
> Thanks,
>
> Sean
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden