美文网首页
GCD之dispatch

GCD之dispatch

作者: 你飞跃俊杰 | 来源:发表于2020-01-08 13:37 被阅读0次

    异步
    dispatch_async
    同步
    dispatch_syn
    获取线程
    dispatch_get

    常用的多线程

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 耗时的操作
        dispatch_async(dispatch_get_main_queue(), ^{
            // 更新界面
        });
    });
    

    组队列(dispatch_group):

    //获取队列queue
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //创建多线程分组group
    dispatch_group_t group = dispatch_group_create();
    //将分组任务添加到队列中
     dispatch_group_enter(group);//+1
    dispatch_group_async(group, queue, ^{ 
    dispatch_group_leave(group);//-1
     });
     dispatch_group_enter(group);//+1
    dispatch_group_async(group, queue, ^{
    dispatch_group_leave(group);//-1
     });
     dispatch_group_enter(group);//+1
    dispatch_group_async(group, queue, ^{ 
    dispatch_group_leave(group);//-1
    }); 
    //等待所有分组任务执行完之后再执行
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            // =0
    });
    

    就是将多线程加入到队列中queue,等所有分组group任务执行完便会收到通知


    WeChat7b7ba359f012b3d5cb8f6b7680f8ac1c.png

    阻塞任务(dispatch_barrier):

    /* 创建并发队列 */
    dispatch_queue_t concurrentQueue = dispatch_queue_create("test.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
    /* 添加两个并发操作A和B,即A和B会并发执行 */
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"OperationA");
    });
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"OperationB");
    });
    /* 添加barrier障碍操作,会等待前面的并发操作结束,并暂时阻塞后面的并发操作直到其完成 */
    dispatch_barrier_async(concurrentQueue, ^(){
        NSLog(@"OperationBarrier!");
    });
    /* 继续添加并发操作C和D,要等待barrier障碍操作结束才能开始 */
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"OperationC");
    });
    dispatch_async(concurrentQueue, ^(){
        NSLog(@"OperationD");
    });
    

    dispatch_barrier_async可以强制使之后的线程,等待之前的线程,等之前的所有线程都执行完再继续

    信号量: dispatch_semaphore_t

    dispatch_semaphore_create用来创建一个semaphore信号量并设置初始信号量的值;
    dispatch_semaphore_signal发送一个信号让信号量增加1(对应PV操作的V操作);
    dispatch_semaphore_wait等待信号使信号量减1(对应PV操作的P操作);
    
    /* 创建一个信号量 */
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    /* 任务1 */
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        /* 耗时任务1 */
        NSLog(@"任务1开始");
        [NSThread sleepForTimeInterval:3];
        NSLog(@"任务1结束");
        /* 任务1结束,发送信号告诉任务2可以开始了 */
        dispatch_semaphore_signal(semaphore);
    });
    
    /* 任务2 */
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        /* 等待任务1结束获得信号量, 无限等待 */
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        /* 如果获得信号量则开始任务2 */
        NSLog(@"任务2开始");
        [NSThread sleepForTimeInterval:3];
        NSLog(@"任务2结束");
    });
    [NSThread sleepForTimeInterval:10];
    

    相关文章

      网友评论

          本文标题:GCD之dispatch

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