zoukankan      html  css  js  c++  java
  • GCD(2)

    dispatch_sync

    dispatch_async函数的”async”意味着非同步,就是将指定的Block非同步的追加到指定的Dispatch Queue中,disptch_async函数不做任何等待;相对应的”sync”意味着同步,也就是说将指定的Block同步追加到Dispatch Queue中,在追加Block结束前,dispatch_sync函数会一直等待

    应用情景:执行Main Dispatch Queue时,使用另外的线程Global Dispatch Queue进行处理,处理结束后立即使用所得到的结果

    dispatch_queue_t  queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

    dispatch_sync(queue,^{/*处理*/});

    注意:死锁问题

    dispatch_queue_t  queue = dispatch_get_main_queue()

    dispatch_sync(queue,^{NSLog(@"Hello ?")});

    dispatch_queue_t  queue = dispatch_queue_create("com,example.gcd.MySerialDispachQueue", NULL);

        dispatch_async(queue,^{

            dispatch_sync(queue, ^{

                NSLog(@"hello");

            })

    });

     

    dispatch_apply

    该函数按指定的次数将指定的Block追加到指定的Dispatch Queue中,并等待全部处理执行结束

    dispatch_queue_t  queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

     dispatch_apply(10, queue, ^(size_t index) {

            NSLog(@"%zu",index);

        });

    NSLog(@"done");

    dispatch_suspend/dispatch_resume

    当追加大量处理到Dispatch Queue时,在追加处理的过程中,有时希望不执行已追加的处理,这时只要挂起Dispatch Queue,当可以执行时再恢复

    //挂起

    dispatch_suspend(queue);

    //恢复

    dispatch_resume(queue);

    Dispatch Semaphore

    是持有计数的信号,该计数是多线程编程中的计数类型信号。计数值为0时等待,计数为1或大于1时,减去1而不等待

    //create

    dispatch_semaphore_t semaphore =dispatch_semaphore_create(1);

     

    //持有

    dispatch_retain(semaphore);

     

    //释放

    dispatch_release(semaphore);

     

    //阻塞等待

       dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);

        dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 1ull*NSEC_PER_SEC);

        long result = dispatch_semaphore_wait(semaphore, time);

        if (result == 0) {

            /*计数值大于0*/

        }else{

            /*等待time时间后,计数值仍然为0*/

    }

    在等待时间内,接收到single消息 dispatch_semaphore_wait立即返回

     

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);

        NSMutableArray *array = [[NSMutableArray alloc]init];

        for (int i = 0; i < 1000; i++) {

            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

            [array addObject:[NSNumber numberWithInt:i]];

            dispatch_semaphore_signal(semaphore);

        }

    dispatch_release(semaphore);

     

    dispatch_once

    dispatch_once函数是保证在应用程序执行中只执行一次指定处理的API

    static dispatch_once_t pred;

    dispatch_once(&pred,^{

    /*初始化*/

    });

    单例模式:

    + (AnObject *)sharedSnashotRecordsMgr

    {

        static AnObject *_sharedInstance = nil;

        static dispatch_once_t predicate;

        dispatch_once(&predicate, ^{

            _sharedInstance = [[AnObject alloc] init];

        });

        return _sharedInstance;

    }

     

  • 相关阅读:
    mybatis中的配置文件的约束
    win10下PHP开发环境搭建
    装饰器的理解
    在iis上添加woff字体文件读取
    转发:使用sql命令查询视图中所有引用的基础表
    转:C4项目中验证用户登录一个特性就搞定
    转载:NSobject官方介绍
    thinkphp生命周期
    array_intersect_assoc — 带索引检查计算数组的交集
    array_flip — 交换数组中的键和值
  • 原文地址:https://www.cnblogs.com/PJXWang/p/4917478.html
Copyright © 2011-2022 走看看