Re: Thread Safety - A Theoretical Question
Re: Thread Safety - A Theoretical Question
- Subject: Re: Thread Safety - A Theoretical Question
- From: Dave Keck <email@hidden>
- Date: Mon, 12 Oct 2009 11:47:12 -1000
> In that case is it my responsibility to guard all access to my class' thread
> unsafe ivars (like mutable objects for example) or is it the responsibility
> of the user of my class to guard all access to my class' instances in his
> code?
When dealing with Cocoa, the rule is that all classes must be assumed
usable from the main thread only, unless they explicitly allow
otherwise. In my own code, I adopted this rule too; if a class is
thread-safe, I document it as such. If not, I may or may not mention
it; if not, then it's assumed that that class must be used from the
main thread only.
I recommend adopting this rule too, and only making classes thread
safe if it makes sense. While making every class you ever write
thread-safe might be a good intellectual exercise, it's hard and time
consuming, and I doubt there's many of us who do it.
> With regards to this, should I care about thread safety at all? If you are
> designing a class, even without any concurrency in it at all, where do you
> decide to worry about thread safety in code outside your reach?
You should never not care about thread safety! :) Of course that
doesn't mean every method you write should be thread safe. Rather, you
should make a conscious decision of whether a class is going to be
thread-safe, write it as such, and document that fact in the header or
elsewhere. If a user chooses to ignore it and use that class from 5
different threads, then the bug's in their code, not yours.
_______________________________________________
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