Friday, April 19, 2013

NSConditionLock unlockWithCondition example objc


unlockWithCondition:

Relinquishes the lock and sets the receiver’s condition.
- (void)unlockWithCondition:(NSInteger)condition
Parameters
condition
The user-defined condition for the lock. The value of condition is user-defined; see the class description for more information.

Example of unlockWithCondition

#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 unlockWithCondition