美文网首页
dispatch_barrier_async 与dispatch

dispatch_barrier_async 与dispatch

作者: 言霏 | 来源:发表于2020-04-16 14:14 被阅读0次

1. dispatch_barrier_async

例子1.1:

- (void)testGCD_Barrier {
    
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"Task 1,%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"Task 2,%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"Task 3,%@",[NSThread currentThread]);
    });
    dispatch_barrier_async(queue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"barrier");
    });
    NSLog(@"running1, %@", [NSThread currentThread]);
    
    dispatch_async(queue, ^{
        NSLog(@"Task 4,%@",[NSThread currentThread]);
    });
    NSLog(@"running2, %@", [NSThread currentThread]);
    dispatch_async(queue, ^{
        NSLog(@"Task 5,%@",[NSThread currentThread]);
    });
}

结果1.1:

2020-04-16 12:42:22.483987+0800 Task 3,<NSThread: 0x600003cdeac0>{number = 4, name = (null)}
2020-04-16 12:42:22.483983+0800 running1, <NSThread: 0x600003cc60c0>{number = 1, name = main}
2020-04-16 12:42:22.483986+0800 Task 2,<NSThread: 0x600003c99e00>{number = 6, name = (null)}
2020-04-16 12:42:22.484120+0800 running2, <NSThread: 0x600003cc60c0>{number = 1, name = main}
2020-04-16 12:42:23.484001+0800 Task 1,<NSThread: 0x600003ca9e40>{number = 7, name = (null)}
2020-04-16 12:42:24.487740+0800 barrier
2020-04-16 12:42:24.488144+0800 Task 4,<NSThread: 0x600003ca9e40>{number = 7, name = (null)}
2020-04-16 12:42:24.488144+0800 Task 5,<NSThread: 0x600003c6bb40>{number = 8, name = (null)}

结论1.1:

使用dispatch_queue_create 的话

  1. Task1、2、3在子线程异步执行,都在barrier的前面,Task4、5在barrier的后面异步执行。
  2. barrier会等待Task1、2、3都执行完毕才会执行。
  3. running1和running2不必等待barrier执行完就在当前线程(主线程)执行了。

例子1.2:

- (void)testGCD_Barrier {
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"Task 1,%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"Task 2,%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"Task 3,%@",[NSThread currentThread]);
    });
    dispatch_barrier_async(queue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"barrier");
    });
    NSLog(@"running1, %@", [NSThread currentThread]);
    
    dispatch_async(queue, ^{
        NSLog(@"Task 4,%@",[NSThread currentThread]);
    });
    NSLog(@"running2, %@", [NSThread currentThread]);
    dispatch_async(queue, ^{
        NSLog(@"Task 5,%@",[NSThread currentThread]);
    });
}

结果1.2:

 2020-04-16 12:33:17.270059+0800 running1, <NSThread: 0x600002f40540>{number = 1, name = main}
2020-04-16 12:33:17.270197+0800 running2, <NSThread: 0x600002f40540>{number = 1, name = main}
2020-04-16 12:33:18.287278+0800 Task 1,<NSThread: 0x600002f40540>{number = 1, name = main}
2020-04-16 12:33:18.287470+0800 Task 2,<NSThread: 0x600002f40540>{number = 1, name = main}
2020-04-16 12:33:18.287596+0800 Task 3,<NSThread: 0x600002f40540>{number = 1, name = main}
2020-04-16 12:33:19.288755+0800 barrier
2020-04-16 12:33:19.289002+0800 Task 4,<NSThread: 0x600002f40540>{number = 1, name = main}
2020-04-16 12:33:19.289125+0800 Task 5,<NSThread: 0x600002f40540>{number = 1, name = main}

结论1.2:

使用dispatch_get_main_queue 的话

  1. Task1、2、3、barrier、4、5就直接在主线程顺序执行了。
  2. 因为Task1让线程(此时就是主线程)休眠了1秒,所以Task2、3也就在Task1后执行。
  3. 一般也不会搭配dispatch_get_main_queue使用,没啥实际意义。

2. dispatch_barrier_sync

例子2.1:

- (void)testGCD_Barrier {
    
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"Task 1,%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"Task 2,%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"Task 3,%@",[NSThread currentThread]);
    });
    dispatch_barrier_sync(queue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"barrier");
    });
    NSLog(@"running1, %@", [NSThread currentThread]);
    
    dispatch_async(queue, ^{
        NSLog(@"Task 4,%@",[NSThread currentThread]);
    });
    NSLog(@"running2, %@", [NSThread currentThread]);
    dispatch_async(queue, ^{
        NSLog(@"Task 5,%@",[NSThread currentThread]);
    });
}

结果2.1:

2020-04-16 12:45:04.678625+0800 Task 2,<NSThread: 0x6000009bbac0>{number = 4, name = (null)}
2020-04-16 12:45:04.678638+0800 Task 3,<NSThread: 0x600000997900>{number = 6, name = (null)}
2020-04-16 12:45:05.678649+0800 Task 1,<NSThread: 0x6000009a17c0>{number = 3, name = (null)}
2020-04-16 12:45:06.679074+0800 barrier
2020-04-16 12:45:06.679572+0800 running1, <NSThread: 0x6000009e2680>{number = 1, name = main}
2020-04-16 12:45:06.679873+0800 running2, <NSThread: 0x6000009e2680>{number = 1, name = main}
2020-04-16 12:45:06.679975+0800 Task 4,<NSThread: 0x6000009a2680>{number = 5, name = (null)}
2020-04-16 12:45:06.681213+0800 Task 5,<NSThread: 0x6000009a17c0>{number = 3, name = (null)}

结论2.1:

使用dispatch_queue_create的话

  1. Task1、2、3在子线程异步执行,都在barrier的前面,Task4、5在barrier的后面异步执行。
  2. running1和running2必须等待barrier执行完才会在当前线程(主线程)执行,也就是dispatch_barrier_sync会阻塞当前线程,因此dispatch_barrier_sync 也就可能造成线程锁死。

例子2.2:

- (void)testGCD_Barrier {
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"Task 1,%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"Task 2,%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"Task 3,%@",[NSThread currentThread]);
    });
    dispatch_barrier_sync(queue, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"barrier");
    });
    NSLog(@"running1, %@", [NSThread currentThread]);
    
    dispatch_async(queue, ^{
        NSLog(@"Task 4,%@",[NSThread currentThread]);
    });
    NSLog(@"running2, %@", [NSThread currentThread]);
    dispatch_async(queue, ^{
        NSLog(@"Task 5,%@",[NSThread currentThread]);
    });
}

结果2.2:
直接就崩了


image.png

结论2.2:

  1. dispatch_barrier_sync不能与dispatch_get_main_queue结合使用。

注意事项:

  • dispatch_barrier_(a)sync一般搭配自定义并行队列 dispatch_queue_t 使用。
  • 如果搭配dispatch_get_global_queue ,其作用和 dispatch_(a)sync 一样了没有实际使用意义。
  • 一般也搭配dispatch_get_main_queue 使用,dispatch_barrier_sync还会导致crash。

相关文章

网友评论

      本文标题:dispatch_barrier_async 与dispatch

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