NSManagedObjectContext lock
===

I'm writing a multi-threaded application using Core Data.

Under Mac OS X 10.6.8, I'm under the impression that `- [NSManagedObjectContext lock]` does not work at all. As a proof of concept here is a sample built after the Xcode 3 "Command-line tool" template, "Core Data" flavor:

    int main (int argc, const char * argv[])
    {
      objc_startCollectorThread();
	
      NSManagedObjectContext *context = managedObjectContext();
  
      [context lock];
      [context lock];
  
      NSLog(@"hello world! (context=%@)", context);
  
      [context unlock];
      [context unlock];
  
      return 0;
    }

That should result in a deadlock, instead I've got this log:

    2012-07-18 16:53:40.206 test[20004:a0b] hello world! (context=<NSManagedObjectContext: 0x20000df40>)

Can anybody tell me what is happening?

(if I use instances of NSLock instead of the context, the deadlock happens as expected)

----------
here is an excerpt from [Apple Documentation about NSManagedObjectContext lock][1]

lock
----

Attempts to acquire a lock on the receiver.

`- (void)lock`

Discussion

This method blocks a thread’s execution until the lock can be acquired. An application protects a critical section of code by requiring a thread to acquire a lock before executing the code. Once the critical section is past, the thread relinquishes the lock by invoking unlock.

Sending this message to a managed object context helps the framework to understand the scope of a transaction in a multi-threaded environment. It is preferable to use the NSManagedObjectContext’s implementation of NSLocking instead using of a separate mutex object.

If you lock (or successfully tryLock) a managed object context, the thread in which the lock call is made must have a retain until it invokes unlock. If you do not properly retain a context in a multi-threaded environment, this will result in deadlock.

Availability
Available in Mac OS X v10.4 and later.


  [1]: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/lock