views:

4608

answers:

3

Does @synchronized not use "lock" and "unlock" to achieve mutual exclusion? How does it do lock/unlock then?

The output of the following program is only "Hello World".

@interface MyLock: NSLock<NSLocking>
@end

@implementation MyLock

- (id)init {
    return [super init];
}

- (void)lock {
    NSLog(@"before lock");
    [super lock];
    NSLog(@"after lock");
}

- (void)unlock {
    NSLog(@"before unlock");
    [super unlock];
    NSLog(@"after unlock");
}

@end


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    MyLock *lock = [[MyLock new] autorelease];
    @synchronized(lock) {
        NSLog(@"Hello World");
    }

    [pool drain];
}
A: 

It just associates a semaphore with every object, and uses that.

Pavel Minaev
Technically, it creates a mutex lock, but the basic idea is correct. See the Apple diva at: http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW16
Mark Bessey
Not just a mutex, but a recursive lock.
kperryua
+10  A: 

The objective C language level synchronization uses the mutex, just like NSLock does. Semantically there are some small technical differences, but it is basically correct to think of them as two seperate interface implemented on top of a common (more primitive) entity.

In particular with an NSLock you have an explicit lock whereas with @synchronize you have an implicit lock associated with the object you are using to synchronize. The benefit of the language level locking is the compiler understands it so it can deal with scoping issues, but mechanically they are the behave basically the same.

You can think of @synchronize as basically a compiler rewrite:

- (NSString *)myString {
  @synchronized(self) {
    return [[myString retain] autorelease];
  }
}

is transformed into:

- (NSString *)myString {
  NSString *retval = nil;
  pthread_mutex_t *self_mutex = LOOK_UP_MUTEX(self);
  pthread_mutex_lock(self_mutex);
  retval = [[myString retain] autorelease];
  pthread_mutex_unlock(self_mutex);
  return retval;
}

That is not exactly correct because the actual transform is more complex and uses recursive locks, but it should get the point across.

Louis Gerbarg
You're also forgetting the exception handling that @synchronized does for you. And as I understand it, much of this is handled at runtime. This allows for optimization on uncontended locks, etc.
Quinn Taylor
Like I said, the actual generated stuff is more complex, but I didn't feel like writing section directives in order build the DWARF3 unwind tables ;-)
Louis Gerbarg
And I can't blame you. :-) Also note that OS X uses Mach-O format instead of DWARF.
Quinn Taylor
No one uses DWARF as a binary format. OS X does use DWARF for debug symbols, and it uses DWARF unwind tables for zero cost exceptions
Louis Gerbarg
For reference, I have written compiler backends for Mac OS X ;-)
Louis Gerbarg
I really cannot comment on the whole DRAWF vs. Mach-O thing (wish I could), but I will say that you left out a 'z' in synchronized ;) Great answer, BTW, and +1
Yar
Heh, thanks, I will fix that
Louis Gerbarg
+4  A: 

In Objective-C, a @synchronized block handles locking and unlocking (as well as possible exceptions) automatically for you. The runtime dynamically essentially generates an NSRecursiveLock that is associated with the object you're synchronizing on. This Apple documentation explains it in more detail. This is why you're not seeing the log messages from your NSLock subclass — the object you synchronize on can be anything, not just an NSLock.

Basically, @synchronized (...) is a convenience construct that streamlines your code. Like most simplifying abstractions, it has associated overhead (think of it as a hidden cost), and it's good to be aware of that, but raw performance is probably not the supreme goal when using such constructs anyway.

Quinn Taylor