self.condition = [[NSCondition alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"-----------1111-----------");
[self.condition lock];
NSLog(@"-----------222-----------");
[self.condition wait]; //调用wait之后代码一直阻塞在这里,同时别人也能获得lock。 当等到signal就会执行后面的代码
NSLog(@"-----------333-----------");
[self.condition unlock];
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"-----------444-----------");
sleep(2);
[self.condition lock];
NSLog(@"-----------555-----------");
sleep(5);
[self.condition signal];
NSLog(@"-----------666-----------");
[self.condition unlock];
});