Re: Threadsafe copy of objective c object
Re: Threadsafe copy of objective c object
- Subject: Re: Threadsafe copy of objective c object
- From: Jeff Kelley <email@hidden>
- Date: Tue, 03 Sep 2013 14:32:15 -0400
Ken is, of course, correct. This is what I get for writing it in my mail
client.
You’ll want to use dispatch_sync() for reads and dispatch_barrier_async()
for writes.
Jeff Kelley
On Tue, Sep 3, 2013 at 2:30 PM, Ken Thomases <email@hidden> wrote:
> On Sep 3, 2013, at 9:26 AM, Jeff Kelley wrote:
>
> > You could use a dedicated dispatch queue for all property access and use
> > dispatch barriers to restrict access to the queue for writes, while still
> > allowing simultaneous reads.
> >
> > In -copy:
> >
> > - (id)copy
> > {
> > __block __typeof(self) copy;
> >
> > dispatch_async(self.propertyQueue, ^{
>
> You can't use dispatch_async() for this. It has to be synchronous, since
> you're returning the value that's going to be set.
>
> > copy = [[[self class] alloc] init];
> > // Copy properties here
> > });
> >
> > return copy;
> > }
> >
> > This assumes a property called propertyQueue:
> >
> > @property (readonly, nonatomic) dispatch_queue_t propertyQueue;
> >
> > - (dispatch_queue_t)propertyQueue
> > {
> > if (!_propertyQueue) {
> > _propertyQueue = dispatch_queue_create("queue name here",
> > DISPATCH_QUEUE_CONCURRENT);
> > }
> >
> > return _propertyQueue;
> > }
> >
> >
> > In your property getters:
> >
> > - (int)count
> > {
> > __block int count;
> >
> > dispatch_async(self.propertyQueue, ^{
>
> Same here.
>
> > count = _count;
> > });
> >
> > return count;
> > }
> >
> >
> > And finally, in your property *setters*:
> >
> > - (void)setCount:(int)count
> > {
> > dispatch_barrier_async(self.propertyQueue, ^{
> > _count = count;
> > });
> > }
>
> Regards,
> 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