Skip to content

Instantly share code, notes, and snippets.

@wuchuwuyou
Created November 16, 2017 08:22
Show Gist options
  • Save wuchuwuyou/c581037660f0439ce5981e93d4076f3d to your computer and use it in GitHub Desktop.
Save wuchuwuyou/c581037660f0439ce5981e93d4076f3d to your computer and use it in GitHub Desktop.
runloop 阻塞线程
@implementation ViewController{
BOOL end;
}
– (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@”start new thread …”);
[NSThread detachNewThreadSelector:@selector(runOnNewThread) toTarget:self withObject:nil];
while (!end) {
NSLog(@”runloop…”);
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
NSLog(@”runloop end.”);
}
NSLog(@”ok.”);
}
(void)runOnNewThread{
NSLog(@”run for new thread …”);
sleep(1);
[self performSelectorOnMainThread:@selector(setEnd) withObject:nil waitUntilDone:NO];
NSLog(@”end.”);
}
-(void)setEnd{
end=YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment