Re: Thread Safety - A Theoretical Question
Re: Thread Safety - A Theoretical Question
- Subject: Re: Thread Safety - A Theoretical Question
- From: Nick Zitzmann <email@hidden>
- Date: Mon, 12 Oct 2009 15:34:51 -0600
On Oct 12, 2009, at 2:47 PM, André Berg wrote:
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?
It depends on whether your class can change states between methods or
not, with the exception of the main get/set method. Usually, internal
locking makes the most sense - unless, for example, you're designing
an array, and want to create a count accessor, and the array could be
accessed from several threads at once, in which case you'd need to
lock it externally or else you'd end up with a possibly inaccurate
count variable.
See NSCache vs. NSMutableDictionary for a good example of this.
NSCache is locked internally, but there's no way to get the state of
the class (for example, the key-value count) other than the object for
a given key. NSMutableDictionary isn't locked internally, but there is
a way to get a count, enumerator, etc. so it's your responsibility to
lock it when necessary.
With regards to this, should I care about thread safety at all?
Yes. It pays to plan ahead for what could happen, because the
alternative is to be stuck with a dumb design decision that can't be
easily taken back later.
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
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