Friday, April 19, 2013

NSConditionLock initWithCondition example objc


initWithCondition:

Initializes a newly allocated NSConditionLock object and sets its condition.
- (id)initWithCondition:(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.
Return Value
An initialized condition lock object; may be different than the original receiver.

Example - initWithCondition
#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

}