Wie sperrt / entsperrt @synchronized in Objective-C?

Verwendet @synchronized nicht "lock" und "unlock", um einen gegenseitigen Ausschluss zu erreichen? Wie wird dann gesperrt / entsperrt?

Die Ausgabe des folgenden Programms ist nur "Hallo Welt".

@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];
}

Antworten auf die Frage(4)

Ihre Antwort auf die Frage