Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 251 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does @synchronized lock/unlock in Objective-C?

#1
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];
}
Reply

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

#3
Actually

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

transforms directly into:

// needs #import <objc/objc-sync.h>
{
objc_sync_enter(self)
id retVal = [[myString retain] autorelease];
objc_sync_exit(self);
return retVal;
}

This API available since iOS 2.0 and imported using...

#import <objc/objc-sync.h>
Reply

#4
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 separate interfaces implemented on top of a common (more primitive) entity.

In particular with a `NSLock` you have an explicit lock whereas with `@synchronized` 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 behave basically the same.

You can think of `@synchronized` as 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.
Reply

#5
Apple's implementation of @synchronized is open source and it can be found [here][1]. Mike ash wrote two really interesting post about this subject:

- [Locks, Thread Safety, and Swift][2]
- [Let's Build @synchronized][3]

In a nutshell it has a table that maps object pointers (using their memory addresses as keys) to `pthread_mutex_t` locks, which are locked and unlocked as needed.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#6
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][1] 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.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through