• 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: @synchronized crashing with ARC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: @synchronized crashing with ARC


  • Subject: Re: @synchronized crashing with ARC
  • From: Charles Srstka <email@hidden>
  • Date: Thu, 31 May 2012 20:34:44 -0500

On May 31, 2012, at 3:28 PM, Lane Roathe wrote:

> Suggestions:
>
> 1. remove the k prefix, by convention that is reserved for constants
>
> 2. don't use the same var name for a local variable as for a global variable
>
> 3. That should be something like:
>
> static VLFContext* myVLFContext = nil;
> ...
> 	if( !myVLFContext )
> 		myVLFContext = [VLFContext alloc] init];
> ...
> 	@synchronized( myVLFContext ) {
> 	...
> 	}

You’ve got a potential race condition in the initializer. Using dispatch_once instead will solve that:

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
	myVLFContext = [[VLFContext alloc] init];
});

@synchronized(myVLFContext) {
	...
}

If you keep having trouble with @synchronized, though, I’d recommend just switching to a pthread_mutex_t, a spin lock, or dispatch_sync. Any of these should have better performance than @synchronized anyway.

Charles
_______________________________________________

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: @synchronized crashing with ARC
      • From: Antonio Nunes <email@hidden>
  • Prev by Date: Re: Sandboxing and calling Launchctl
  • Next by Date: NSXMLParser thread safe?
  • Previous by thread: Re: @synchronized crashing with ARC
  • Next by thread: Re: @synchronized crashing with ARC
  • Index(es):
    • Date
    • Thread