NSCondition的wait其实就是在线程内等待一个信号量, 信号量出现时就继续, 否则一直等下去
也可以用- (BOOL)waitUntilDate:(NSDate *)limit;
这个在给定的时间到达时仍未有信号量出现, 就自动继续了.
如果用户给出信号量来触发继续的话, 会返回1
如果超时触发继续, 返回0
下面见代码, 最后有工程
复制代码
- #import <UIKit/UIKit.h>
@interface testConditionController : UIViewController {
NSCondition* g_Condition1;
NSCondition* g_Condition2;
}
@end
@implementation testConditionController
- (void)clickedButton1 {
[g_Condition1 lock];
[g_Condition1 signal];
[g_Condition1 unlock];
}
- (void)clickedButton2 {
[g_Condition2 lock];
[g_Condition2 signal];
[g_Condition2 unlock];
}
- (void) showAlert:(NSString*)message {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"title" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void) loop:(NSDictionary*)parameters {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSCondition* condition = [parameters objectForKey:@"condition"];
[condition lock];
if(g_Condition1 == condition) {
while(1) {
[condition wait];
[self performSelectorOnMainThread:@selector(showAlert:) withObject:[parameters objectForKey:@"message"] waitUntilDone:NO];
}
} else if(g_Condition2 == condition) {
while(1) {
NSLog(@"%d", [condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]]);
[self performSelectorOnMainThread:@selector(showAlert:) withObject:[parameters objectForKey:@"message"] waitUntilDone:YES];
}
}
[condition unlock];
[pool release];
}
- (id) init
{
if (self = [super initWithNibName:nil bundle:nil]) {
}
return self;
}
- (void)loadView
{
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = contentView;
contentView.backgroundColor = [UIColor whiteColor];
[contentView release];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(100, 100, 120, 40)];
[button1 setTitle:@"button1" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(clickedButton1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(100, 200, 120, 40)];
[button2 setTitle:@"button2" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(clickedButton2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
g_Condition1 = [[NSCondition alloc] init];
g_Condition2 = [[NSCondition alloc] init];
NSDictionary* loopObject1 = [NSDictionary dictionaryWithObjectsAndKeys:g_Condition1, @"condition", @"g_Condition1", @"message", nil];
[NSThread detachNewThreadSelector:@selector(loop:) toTarget:self withObject:loopObject1];
NSDictionary* loopObject2 = [NSDictionary dictionaryWithObjectsAndKeys:g_Condition2, @"condition", @"g_Condition2", @"message", nil];
[NSThread detachNewThreadSelector:@selector(loop:) toTarget:self withObject:loopObject2];
}
- (void)dealloc {
[g_Condition1 release];
[g_Condition2 release];
[super dealloc];
}
@end
@interface SampleAppDelegate : NSObject <UIApplicationDelegate>
@end
@implementation SampleAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
testConditionController* conditionController = [[testConditionController alloc] init];
[window addSubview:conditionController.view];
[window makeKeyAndVisible];
}
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");
[pool release];
return retVal;
}
NSThread NSCondition wait
最新推荐文章于 2022-06-06 20:32:57 发布