NSThread NSCondition wait

NSCondition的wait其实就是在线程内等待一个信号量, 信号量出现时就继续, 否则一直等下去
也可以用- (BOOL)waitUntilDate:(NSDate *)limit; 
这个在给定的时间到达时仍未有信号量出现, 就自动继续了.
如果用户给出信号量来触发继续的话, 会返回1
如果超时触发继续, 返回0
下面见代码, 最后有工程
复制代码
  1. #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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值