Re: @synchronized crashing with ARC
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