Re: @synchronized([someObject class])
Re: @synchronized([someObject class])
- Subject: Re: @synchronized([someObject class])
- From: Wincent Colaiuta <email@hidden>
- Date: Tue, 19 Jun 2007 13:49:52 +0200
El 19/6/2007, a las 13:13, Ruotger Skupin escribió:
is it possible to do @synchronized([someClass class])?
According to the official documentation, you can use @synchronized
with any Objective-C object (<http://developer.apple.com/
documentation/Cocoa/Conceptual/ObjectiveC/Articles/
chapter_4_section_9.html>).
In practice, at least in Panther and Tiger, you can use literally any
pointer (as noted in <objc/objc-sync.h>, pthread_mutex is used under
the covers); but as always it is best to strictly interpret the docs
so that means using only Objective-C objects.
A Class "object" (as returned by [someClass class]) *is* suitable for
use with the @synchronized directive even though a Class is a pointer
to an objc_class struct and an id is a pointer to an objc_object
struct. The docs already linked to above say:
Listing 3-3 shows an example of code that uses self as the mutex to
synchronize access to the instance methods of the current object.
You can take a similar approach to synchronize the class methods of
the associated class, using the Class object instead of self. In
the latter case, of course, only one thread at a time is allowed to
execute a class method because there is only one class object that
is shared by all callers.
For the synchronization to be meaningful the pointer must be truly
unique, so be careful never to use anything which might be nil:
// do not do this!
id foo = nil;
@synchronized(foo)
{
if (foo == nil)
{
// initialize foo
}
}
I hope this helps.
Cheers,
Wincent
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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