Re: Question about NSThread
Re: Question about NSThread
- Subject: Re: Question about NSThread
- From: Ken Thomases <email@hidden>
- Date: Thu, 14 Jul 2011 10:10:46 -0500
On Jul 14, 2011, at 9:08 AM, Eric E. Dolecki wrote:
> I haven't done much research, but if I have a method that does a lot of
> looping, can I just safely bust this off (fire and forget)?
>
> [NSThread detachNewThreadSelector:@selector(generateBigData) toTarget:self
> withObject:nil];
When it comes to threads, you can't ever "just safely" anything. You have to work to make sure your code is thread-safe. For example, your generateBigData method is being invoked on self. Is it accessing any of self's ivars? Is it accessing _any_ state that will be also be accessed from the main thread or any other thread? If so, you have to ensure that such accesses are safe.
What happens if multiple threads try to set an ivar at the same time? What happens if one is trying to read an ivar while the other is setting it? Do you have ivars that are interrelated such that they have to be changed together to be consistent? If so, then what happens if one thread attempts to read them while another has set one but hasn't gotten around to setting the other, yet? Etc.
Thread safety doesn't just happen. You have to design it in and then carefully implement it. There are tons of pitfalls, even for developers who are being deliberate and careful.
Good luck,
Ken
_______________________________________________
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