- (void)viewDidLoad {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
[selftestLock];
});
}
#define semaphore_signal 0
- (void)testLock {
#if semaphore_signal
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
#else
dispatch_group_t group =dispatch_group_create();
#endif
NSLog(@"begin");
#if semaphore_signal
#else
dispatch_group_enter(group);
#endif
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSLog(@"loading....");
[imageView setImageWithURL:[NSURL URLWithString:@"http://www.pptbz.com/pptpic/UploadFiles_6909/201110/20111014111307895.jpg"] placeholder:nil options:0 completion:^(UIImage*image,NSURL*url,YYWebImageFromType from,YYWebImageStage stage,NSError*error) {
NSLog(@"loading finish ... ");
#if semaphore_signal
dispatch_semaphore_signal(sem);
#else
dispatch_group_leave(group);
#endif
}];
});
#if semaphore_signal
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
#else
dispatch_group_wait(group,DISPATCH_TIME_FOREVER);
#endif
NSLog(@"end");
}
2016-08-05 16:38:51.616 TableViewControllerDemo[16337:2393186] begin
2016-08-05 16:38:51.617 TableViewControllerDemo[16337:2393186] loading....
2016-08-05 16:38:51.703 TableViewControllerDemo[16337:2392956] loading finish ...
2016-08-05 16:38:51.704 TableViewControllerDemo[16337:2393186] end