Synchronized as a keyword and property is not available
Edit me
Note: See Threading Programming Guide for more information about @synchronized.
Use mulle-thread for least hassle.
mulle-thread is available on all platforms, that run Objective-C.
Use mulle_thread_mutex_t
to transform
- (void) myFunction
{
@synchronized()
{
}
}
to
static mulle_thread_mutex_t lock;
+ (void) load
{
mulle_thread_mutex_init( &lock);
}
- (void) myFunction
{
mulle_thread_mutex_lock( &lock);
{
}
mulle_thread_mutex_unlock( &lock);
}
Use NSLock instead
static NSLock lock;
+ (void) initialize
{
if( ! lock)
lock = [[NSLock alloc] init];
}
+ (void) deinitialize
{
if( lock)
{
[lock release];
lock = nil;
}
}
- (void) myFunction
{
[lock lock];
{
}
[lock unlock];
}
Good points
- code works in all runtimes without another dependency
+deinitialize
will not be called by other runtimes, it’s a harmless addition
Bad points
- A NSLock is slower than a
mulle_thread_mutex_t
- The lock has not become a proper mulle-objc root object, so this code will leak in tests.
You could fix this with deleting +deinitialize
and rewriting +initialize
as:
+ (void) initialize
{
if( ! lock)
{
lock = [[NSLock alloc] init];
#ifdef __MULLE_OBJC__
[lock _becomeRootObject];
[lock release;]
#endif
}
}
// + (void) deinitialize clashes with _becomeRootObject and must be removed