Re: Threadsafe copy of objective c object
Re: Threadsafe copy of objective c object
- Subject: Re: Threadsafe copy of objective c object
- From: Graham Cox <email@hidden>
- Date: Tue, 03 Sep 2013 14:22:55 +0200
On 03/09/2013, at 1:23 PM, Jonathan Taylor <email@hidden> wrote:
> Ah, that's a good point about implementing -copy myself. However, how would @synchronized(self){…..} help there? Surely all that would do is prevent multiple threads from calling 'copy' simultaneously - which as far as I am aware isn't something I should be worried about. My understanding is that it would have no impact on whether for example the copied object contains correct values.
It wasn't really clear from your post where it needs to be thread safe. It sounded as though you wanted to call copy on the "original" object from multiple threads. As Lasse pointed out, that's not enough if the copied values can be changed.
> If it's relevant, I should add that the object's properties are all simple types (double, int, bool etc).
If they are declared as atomic, I believe you can trust that simple types are thread safe. It's when they are objects that things can still mutate mid-way through a setter.
> It doesn't help that I'm not really sure what window conditions I am guarding against in my desire for thread safety, which makes my question a little vague.
Well, indeed.
If you are making a copy of the object for the purpose of passing it as a set of parameters to some other code, then presumably that object, once copied, is self-contained and is used on a single thread from then on. That was the scenario I sort of assumed from your original post. If that's not the case then you need to make the problem clearer, i.e. is the "algorithm" multi-threaded in itself?
--Graham
_______________________________________________
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