Showing posts with label NSConditionLock. Show all posts
Showing posts with label NSConditionLock. Show all posts

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





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

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

}

NSConditionLock example objc


Overview

The NSConditionLock class defines objects whose locks can be associated with specific, user-defined conditions. Using an NSConditionLock object, you can ensure that a thread can acquire a lock only if a certain condition is met. Once it has acquired the lock and executed the critical section of code, the thread can relinquish the lock and set the associated condition to something new. The conditions themselves are arbitrary: you define them as needed for your application.
Example of NSConditionLock

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