Return to the Alphabetic Index
Return to the Class Browser
Return to the Picture Browser
Copyright (c) 1994 by NeXT Computer, Inc. All Rights Reserved.

NSRecursiveLock

Inherits From: NSObject

Conforms To: NSLocking NSObject (NSObject)

Declared In: Foundation/NSLock.h

Class Description

NSRecursiveLock is used for locks that need to be reacquired by the same thread.

An NSRecursiveLock locks a critical section of code such that a single thread can reaquire the lock multiple times without deadlocking, while preventing access by other threads. (Note that this implies that a recursive lock will not protect a critical section from a signal handler interrupting the thread holding the lock.) Here is an example where a recursive lock functions properly but other lock types would deadlock:

// create the lock only once!

NSRecursiveLock *theLock = [NSRecursiveLock new];

/* ...other code... */

[theLock lock];

/* ... possibly a long time of fussing with global data... */

[theLock lock]; /* possibly invoked in a subroutine */

[theLock unlock];

[theLock unlock];

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