Friday, April 19, 2013

NSConditionLock lockWhenCondition example objc


lockWhenCondition:

Attempts to acquire a lock.
- (void)lockWhenCondition:(NSInteger)condition
Parameters
condition
The condition to match on.
Discussion on lockWhenCondition
The receiver’s condition must be equal to condition before the locking operation will succeed. This method blocks the thread’s execution until the lock can be acquired.
Example of lockWhenCondition
#import <Foundation/Foundation.h>
#define NOT_DONE 0
#define DONE 1

// this will need to be on some class that is calling the NSThread object
-(void) someFunc:(id) arg {
NSConditionLock* myLock = arg;
[myLock lock];
//do stuff that we need to know about in main
[myLock unlockWithCondition:DONE];
}
int main() {
NSConditionLock* finishedLock = [[NSConditionLock alloc]
initWithCondition: NOT_DONE];

NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(someFunc:) object:finishedLock];
[myThread start];
[finishedLock lockWhenCondition:DONE];
// now we can do whatever we need to do with the results from
// the thread we spawned
}
Example of lockWhenCondition