ftp.nice.ch/peanuts/GeneralData/Documents/openstep/OpenStepSpec_rtf.tar.gz#/OpenStepSpec_rtf/FoundationKit/Classes/NSConditionLock.rtf

This is NSConditionLock.rtf in view mode; [Download] [Up]

paperh18648 margl-907 margr0 margt0 margb0 fi0 ri0 ql sb0 f1 fs24 Copyright f3 'e3f1 1994 by NeXT Computer, Inc.  All Rights Reserved.

s4 li100 fi0 ri1007 ql f0 b fs36 fs100 
fs36 NSConditionLock 
pard s11 li2872 fi-2771 ri1007 ql tx2872 f1 b0 fs28 fs48 
fs28 f0 b fs24 Inherits From:tab b0 fs28 f1 NSObject
fs20 
fs28 s6 f0 b fs24 Conforms To:tab b0 fs28 f1 NSLocking
fi0 NSObject (NSObject)
fs20 
fs28 s7 fi-2771 f0 b fs24 Declared In:tab b0 fs28 f1 Foundation/NSLock.h 
fs20 
fs28 pard s16 li100 fi0 ri1007 ql f0 b fs28 fs20 
fs28 Class Description
fs14 
fs28 pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 b0 fs2 
fs28 NSConditionLock objects are used to lock and unlock threads when specified conditions occur.
fs16 
fs28 The user of an NSConditionLock object can lock when a process enters a particular state and can set the state to something else when releasing the lock. The states are defined by the lock'27s user. NSConditionLock is well suited to synchronizing different modules such as a producer and a consumer where the two modules must share data, but the consumer must sleep until a condition is met such as more data being available.
fs16 
fs28 The NSConditionLock class provides four ways of locking its objects (b lockb0 ,b  lockWhenCondition:b0 ,b  tryLockb0 , and b tryLockWhenConditionb0 ) and two ways of unlocking (b unlock b0 andb  unlockWithCondition:b0 ). Any combination of locking method and unlocking method is legal. 
fs16 
fs28 The following example shows how the producer-consumer problem might be handled using condition locks. The producer need not wait for a condition, but must wait for the lock to be made available so it can safely create shared data. For example, a producer could use a lock this way:
fs16 
fs28 pard s5 li1231 fi0 ri1007 ql f2 fs20 /* create the lock only once */
id condLock = [NSConditionLock new];

[condLock lock];
/* Manipulate global data... */
[condLock unlockWithCondition:HAS_DATA];
pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 fs28 fs16 
fs28 Multiple consumer threads can then lock until there'27s data available and everyone is out of locked critical sections. In the following code sample, the consumer sleeps until the producer invokes b unlockWithCondition:b0  with the parameter HAS_DATA:
fs16 
fs28 pard s5 li1231 fi0 ri1007 ql f2 fs20 [condLock lockWhenCondition:HAS_DATA];
/* Manipulate global data if necessary... */
[condLock unlockWithCondition:(moreData ? HAS_DATA : NO_DATA)];
pard s1 li477 fi0 ri1007 ql tx2494 tx2872 tx3250 f1 fs28 fs16 
fs28 An NSConditionLock object doesn'27t busy-wait, so it can be used to lock time-consuming operations without degrading system performance.
fs16 
fs28 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.
fs16 
fs28 pard s16 li100 fi0 ri1007 ql f0 b fs24 
fs28 Initializing an NSConditionLock
fs14 
fs28 pard s8 li7029 fi-6552 ri1007 ql tx6652 tx7030 f1 b0 fs2 
fs28 fi-6552 {f3 -} (id)b initWithCondition:b0 (int)i conditioni0 tab Initializes a newly created NSConditionLock and sets its condition to i conditioni0 .
pard s16 li100 fi0 ri1007 ql f0 b fs48 
fs28 Returning the Condition
fs14 
fs28 pard s8 li7029 fi-6552 ri1007 ql tx6652 tx7030 f1 b0 fs2 
fs28 fi-6552 {f3 -} (int)b conditionb0 tab Returns the receiver'27s condition, the state that must be achieved before a conditional lock can be acquired or released.
pard s16 li100 fi0 ri1007 ql f0 b fs48 
fs28 Acquiring and Releasing a Lock
fs14 
fs28 pard s8 li7029 fi-6552 ri1007 ql tx6652 tx7030 f1 b0 fs2 
fs28 fi-6552 {f3 -} (void)b lockWhenCondition:b0 (int)i conditioni0 tab Attempts to acquire a lock when i conditioni0  is met. Blocks until i conditioni0  is met.
fi-6552 fs16 
fs28 fi-6552 {f3 -} (void)b unlockWithCondition:b0 (int)i conditioni0 tab Releases the lock and sets lock state to i conditioni0 .
fi-6552 fs16 
fs28 fi-6552 {f3 -} (BOOL)b tryLockb0 tab Attempts to acquire a lock. Returns YES if successful and NO otherwise.
fi-6552 fs16 
fs28 fi-6552 {f3 -} (BOOL)b tryLockWhenCondition:b0 (int)i conditioni0 tab Attempts to acquire a lock when i conditioni0  is met. Returns YES if successful and NO otherwise.
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.