IOS GCD线程相关内容(dispatch_barrier_sync,dispatch_barrier_async)

前言:
barrier:意思是屏障、阻碍的意思,那么它是不是在在GCD中设置阻碍用的呢?
可以先看下这一篇关于dispatch_sync、dispatch_async

1、串行队列 + 同步任务 + barrier_sync

/*
 串行队列 + 同步任务 + barrier_sync :没有开启新线程,都是逐一执行
 */
- (void)serialSyncBarrierSync{

    dispatch_queue_t queue = dispatch_queue_create("com.pi2e.com", DISPATCH_QUEUE_SERIAL);

     NSLog(@"串行队列 + 同步任务 + barrier_sync----%@",[NSThread currentThread]);
    //在队列里添加同步任务
    dispatch_sync(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---1--%@",[NSThread currentThread]);
        }
    });

    dispatch_sync(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---2--%@",[NSThread currentThread]);
        }
    });

    dispatch_barrier_sync(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---10000--%@",[NSThread currentThread]);
        }

        for(int  i=0 ; i<count;i++){
            NSLog(@"---AAAA--%@",[NSThread currentThread]);
        }
    });

    NSLog(@"----main--- %@",[NSThread currentThread]);

    dispatch_sync(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---3--%@",[NSThread currentThread]);
        }
    });

}

看下打印结果:这跟我们上一篇说的串行队列+同步任务:没有开启新线程,都是逐一执行
这里写图片描述

串行队列+同步任务:没有开启新线程,都在主线程中执行,按着添加任务的顺序逐一执行

2、串行队列 + 同步任务 + barrier_async

/*
 串行队列 + 同步任务 + barrier_async:都逐一执行,spatch_barrier_sync不阻碍主线程任务执行
 */
- (void)serialASyncBarrierASync{

    dispatch_queue_t queue = dispatch_queue_create("com.pi2e.com", DISPATCH_QUEUE_SERIAL);

    NSLog(@"串行队列 + 同步任务 + barrier_async----%@",[NSThread currentThread]);
    //在队列里添加同步任务
    dispatch_sync(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---1--%@",[NSThread currentThread]);
        }
    });

    dispatch_sync(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---2--%@",[NSThread currentThread]);
        }
    });

    dispatch_barrier_async(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---10000--%@",[NSThread currentThread]);
        }

        for(int  i=0 ; i<count;i++){
            NSLog(@"---AAAA--%@",[NSThread currentThread]);
        }
    });

    NSLog(@"----main--- %@",[NSThread currentThread]);


    dispatch_sync(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---3--%@",[NSThread currentThread]);
        }
    });

}

看下打印结果:
这里写图片描述

1、dispatch_barrier_async没有阻碍添加它的线程(此时是主线程)的代码执行(打印main)
2、dispatch_barrier_async前的由于是串行队列 + 同步任务,所以没有开启新线程,逐一执行
3、dispatch_barrier_async之后的任务等它自己执行完成了之后才执行

3、 并行队列 + 异步 + barrier_sync

/*
 * 并行队列 + 异步 + barrier_sync:barrier_sync前的任务并发执行,asyncbarrier后的任务必须等asyncbarrier中的任务执行完成之后才会执行他们,也会阻塞主线程的任务
 */
- (void)ConCurrentQueueDisAsynBarriersyn{

    dispatch_queue_t queue = dispatch_queue_create("com.pi2e.com", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"并发队列 + 异步 + asyncbarrier----%@",[NSThread currentThread]);
    //队列中添加异步任务
    dispatch_async(queue, ^{

        for (int i=0; i<5; i++) {
            NSLog(@"--1--%@",[NSThread currentThread]);
        }
    });

    dispatch_async(queue, ^{

        for (int i=0; i<5; i++) {
            NSLog(@"--2--%@",[NSThread currentThread]);
        }
    });

    dispatch_barrier_sync(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---BBBB--%@",[NSThread currentThread]);
        }

        for(int  i=0 ; i<count;i++){
            NSLog(@"---AAAA--%@",[NSThread currentThread]);
        }
    });

    NSLog(@"----main--- %@",[NSThread currentThread]);
    dispatch_async(queue, ^{

        for (int i=0; i<5; i++) {
            NSLog(@"--3--%@",[NSThread currentThread]);
        }
    });

    dispatch_async(queue, ^{

        for (int i=0; i<5; i++) {
            NSLog(@"--4--%@",[NSThread currentThread]);
        }
    });
}

打印结果

1、dispatch_barrier_sync之前的是并发队列 + 异步,所以会开启新线程、并发执行相关人物
2、dispatch_barrier_sync阻碍了它后面任务执行,都是等它执行完了才继续执行的

4、并行队列 + 异步 + barrier_async

/*
 * 并行队列 + 异步 + barrier_async:barrier_async前的任务并发执行,barrier_async后的任务必须等barrier_async中的任务执行完成之后才会执行他们,但是Barrier不能阻塞主线程的任务
 */
- (void)ConCurrentQueueDisAsynBarrierAsyn{

    dispatch_queue_t queue = dispatch_queue_create("com.pi2e.com", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"并发队列 + 异步 + asyncbarrier----%@",[NSThread currentThread]);
    //队列中添加异步任务
    dispatch_async(queue, ^{

        for (int i=0; i<5; i++) {
            NSLog(@"--1--%@",[NSThread currentThread]);
        }
    });

    dispatch_async(queue, ^{

        for (int i=0; i<5; i++) {
            NSLog(@"--2--%@",[NSThread currentThread]);
        }
    });

    dispatch_barrier_async(queue, ^{

        for(int  i=0 ; i<count;i++){
            NSLog(@"---BBBB--%@",[NSThread currentThread]);
        }

        for(int  i=0 ; i<count;i++){
            NSLog(@"---AAAA--%@",[NSThread currentThread]);
        }
    });

     NSLog(@"----main--- %@",[NSThread currentThread]);
    dispatch_async(queue, ^{

        for (int i=0; i<5; i++) {
            NSLog(@"--3--%@",[NSThread currentThread]);
        }
    });

    dispatch_async(queue, ^{

        for (int i=0; i<5; i++) {
            NSLog(@"--4--%@",[NSThread currentThread]);
        }
    });
}

这里写图片描述

1、dispatch_barrier_async前添加的任务是并发+异步的,所以之前会开启多线程、并发执行
2、dispatch_barrier_async不会阻塞其后的添加它线程的任务,但是会阻塞其他任务,可以看到打印main的日志跑到dispatch_barrier_async打印之前去了

总结:

1、dispatch_barrier_sync(queue,block),queue是串行的,看下queue队列中的block,
在barrier添加之前的block,barrier的block,barrier添加之后的block,还有一些主线程的任务,那么此时它阻塞它自己之后的任务,它一定会等它之前的block执行完,再执行自己的,自己执行完了,它后面的block才会执行
2、dispatch_barrier_async(queue,block)会阻塞其后的dispatch_async/dispatch_sync的任务,但是不会阻塞添加它的线程(一般主线程)中代码的执行。可以看到,main打印会在barrier的block之前执行,原来它的代码是在barrier之后的

猜你喜欢

转载自blog.csdn.net/ycf03211230/article/details/79572284
今日推荐