美文网首页
并发数据顺序执行

并发数据顺序执行

作者: 指尖的跳动 | 来源:发表于2017-10-25 16:11 被阅读7次

    dispatch_semaphore_t sem = dispatch_semaphore_create(1);

    dispatch_queue_t queue = dispatch_queue_create("testBlock", NULL);

    dispatch_async(queue, ^{

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    NSLog(@"1");

    dispatch_semaphore_signal(sem);

    });

    });

    dispatch_async(queue, ^{

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    NSLog(@"2");

    dispatch_semaphore_signal(sem);

    });

    dispatch_async(queue, ^{

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    NSLog(@"3");

    dispatch_semaphore_signal(sem);

    });

    });

    dispatch_async(queue, ^{

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    NSLog(@"4");

    dispatch_semaphore_signal(sem);

    });

    著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。

    相关文章

      网友评论

          本文标题:并发数据顺序执行

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