• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Threadsafe copy of objective c object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Threadsafe copy of objective c object


  • Subject: Re: Threadsafe copy of objective c object
  • From: Ken Thomases <email@hidden>
  • Date: Tue, 03 Sep 2013 13:30:19 -0500

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


  • Follow-Ups:
    • Re: Threadsafe copy of objective c object
      • From: Jeff Kelley <email@hidden>
References: 
 >Threadsafe copy of objective c object (From: Jonathan Taylor <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Graham Cox <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Lasse Jansen <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Diederik Meijer | Ten Horses <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Jonathan Taylor <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Robert Vojta <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Jonathan Taylor <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Robert Vojta <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Jonathan Taylor <email@hidden>)
 >Re: Threadsafe copy of objective c object (From: Jeff Kelley <email@hidden>)

  • Prev by Date: Re: bugreport.apple.com
  • Next by Date: Re: Threadsafe copy of objective c object
  • Previous by thread: Re: Threadsafe copy of objective c object
  • Next by thread: Re: Threadsafe copy of objective c object
  • Index(es):
    • Date
    • Thread