Inherits From: NSObject
Conforms To: NSLocking NSObject (NSObject)
Declared In: Foundation/NSLock.h
An NSLock is used to protect critical regions of code. A lock is created once and is subsequently used to protect one or more regions of code. If a region of code is in use, an NSLock waits using the condition_wait() function, so the thread doesn't busy-wait. The following example shows the use of an NSLock with the methods lock and unlock defined in the NSLocking protocol:
NSLock *theLock = [NSLock new]; // done once!
/* ... other code */
/* ... possibly a long time of fussing with global data... */
The NSConditionLock, NSLock, and NSRecursiveLock classes all implement the NSLocking protocol with various features and performance characteristics; see the other class descriptions for more information.
Acquiring a Lock