美文网首页
dispatch_semaphore_t与dispatch_gr

dispatch_semaphore_t与dispatch_gr

作者: rlqs | 来源:发表于2016-08-05 16:43 被阅读74次

    - (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

    相关文章

      网友评论

          本文标题:dispatch_semaphore_t与dispatch_gr

          本文链接:https://www.haomeiwen.com/subject/ejohsttx.html