Enterprise Objects Framework Release 1.1 Copyright ã1995 by NeXT Computer, Inc.  All Rights Reserved.

NSLock 

Inherits From:	NSObject 

Conforms To:	NSLocking
NSObject (NSObject)

Declared In:	foundation/NSLock.h 

Class Description

The NSLock class defines an object that coordinates the operation of multiple threads of execution within the same application. An NSLock object can be used to mediate access to an application's global data or to protect a critical section of code, allowing it to run atomically.

An NSLock object represents a lock that can be acquired by only a single thread at a time. When one thread has acquired the lock, any other thread is prevented from doing so until the owner relinquishes the lock. An application can have multiple NSLock objects, each protecting different sections of code. However, each NSLock must be created before the application becomes multithreaded.

The basic interface to NSLock is declared by the NSLocking protocol, which declares the lock and unlock methods. To this base, the NSLock class adds the tryLock and lockBeforeDate: methods. The lock method declared in the protocol doesn't return until it is successful. The methods declared in this class add more flexible means of acquiring a lock.

An NSLock could be used to coordinate the updating of a visual display shared by a number of threads involved in a single calculation:

BOOL moreToDo = YES;
NSLock  *theLock = [[NSLock alloc] init];
...
while (moreToDo) {
	if ([theLock tryLock]) {
		/* update display used by all threads */
		[theLock unlock];
	}
	/* do another increment of calculation */
	/* until there's no more to do */
}

The NSLock, NSConditionLock, and NSRecursiveLock classes all implement the NSLocking protocol and offer various additional features and performance characteristics. See the NSConditionLock and NSRecursiveLock class descriptions for more information.

Adopted Protocols

NSLocking	- lock
- unlock

Method Types

Acquiring a Lock	- lockBeforeDate:

Instance Methods

lockBeforeDate:

- (BOOL)lockBeforeDate:(NSDate *)limit

Attempts to acquire a lock before the date represented by limit. Returns YES if the lock is acquired within this time limit. Returns NO if the time limit expires before a lock can be acquired.

See also:  - lock (NSLocking protocol), - unlock (NSLocking protocol)