Re: Threadsafe copy of objective c object
Re: Threadsafe copy of objective c object
- Subject: Re: Threadsafe copy of objective c object
- From: Jonathan Taylor <email@hidden>
- Date: Tue, 03 Sep 2013 12:23:07 +0100
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.
If it's relevant, I should add that the object's properties are all simple types (double, int, bool etc).
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. I guess that what I would like is the following:
- [myObject copy] will not crash/raise exceptions/etc when called from a thread other than the main thread
- [myObject copy] returns a copy in which all parameters are 'valid' (i.e. an individual parameter is either the 'old' or the 'new' value in the case where the object is being changed at the time of the copy)
- True thread safety would require a copy that represents an instantaneous snapshot of the state of the entire object, i.e. copy not taken while object is being updated. Actually, I suspect this last condition is not a problem for my specific case, but best to be on the safe side, for several different reasons.
On 3 Sep 2013, at 12:04, Graham Cox wrote:
>
> On 03/09/2013, at 12:52 PM, Jonathan Taylor <email@hidden> wrote:
>
>> Is there any way, then, that I can take a copy in a threadsafe manner? If necessary I can do the copy on the main thread, but I would prefer not to have to do that for timing reasons. Any suggestions?
>
> Since the implementation of -copy is up to you, you could just put @synchronized(self){…..} around the code in that method. That implements a lock which should make the copy thread-safe.
>
> --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