GCD(二)-dispatch_queue_t dispatch

作者: 司马捷 | 来源:发表于2016-05-25 18:15 被阅读1187次

研究了下dispatch_barrier_async() 顺便把GCD的常用函数都看下.

dispatch_queue_t dispatch_queue_create

文档解释:Creates a new dispatch queue to which blocks can be submitted.
创建一个新的,可以让提交代码块的分发队列.
Parameters:
label
A string label to attach to the queue to uniquely identify it in debugging tools such as Instruments, sample, stackshots, and crash reports. Because applications, libraries, and frameworks can all create their own dispatch queues, a reverse-DNS naming style (com.example.myqueue) is recommended. This parameter is optional and can be NULL.
用于deug时候 绑定在队列上的一个独特的字符串标签,区别系统或者其他框架创建的队列.这个参数可以为空.

attr
In OS X v10.7 and later or iOS 4.3 and later, specify DISPATCH_QUEUE_SERIAL (or NULL) to create a serial queue or specify DISPATCH_QUEUE_CONCURRENT to create a concurrent queue. In earlier versions, you must specify NULL for this parameter.
这个参数 不读不知道,一读吓一跳.
指定 DISPATCH_QUEUE_SERIAL 或者是NULL的时候,创建的队列是串行队列.
指定DISPATCH_QUEUE_CONCURRENT 创建的是并发队列.
在更早的版本中这个参数只能指定NULL

讨论:
Blocks submitted to a serial queue are executed one at a time in FIFO order. Note, however, that blocks submitted to independent queues may be executed concurrently with respect to each other. Blocks submitted to a concurrent queue are dequeued in FIFO order but may run concurrently if resources are available to do so.

代码块被提交到串行队列的执行顺序是一个接着一个的执行,按照先进先出的原则.
然而需要注意的是:代码块被提交到一个独立的队列,可能会彼此并发的执行,
代码块提交到一个并发的队列,也是按照先进先出的顺序,进行出队.但是如果资源允许的话,可能是并发执行.

When your application no longer needs the dispatch queue, it should release it with the dispatch_release function. Any pending blocks submitted to a queue hold a reference to that queue, so the queue is not deallocated until all pending blocks have completed.
当你的程序不再需要这个队列的时候,需要和私用dispathc_release(),去释放这个队列.
任何提交到队列的还没有执行block代码块,都会持有这个队列的引用.所以这个队列并不会被销毁,直到所有的block被执行完毕.

相关文章

  • OC中GCD使用

    一、GCD的使用: dispatch_async(dispatch_queue_t queue, dispatch...

  • 多线程-GCD

    GCD常用API dispatch_queue_t 队列 dispatch_async 和 dispatch_sy...

  • 多线程GCD

    1:GCD 创建队列: 串行队列: dispatch_queue_t queue=dispatch_queue_c...

  • iOS常用代码块demo1

    1.GCD定时器 dispatch_queue_t queue = dispatch_get_global_que...

  • 无标题文章

    GCD学习(五) dispatch_barrier_async 先看段代码 dispatch_queue_t co...

  • GCD研究

    //创建gcd容器(全局队列)并行 dispatch_queue_t queue0 = dispatch_get_...

  • GCD和NSOperation学习笔记

    GCD学习笔记 1.dispatch_sync—同步操作 eg: dispatch_queue_t concurr...

  • GCD(二)-dispatch_queue_t dispatch

    研究了下dispatch_barrier_async() 顺便把GCD的常用函数都看下. 文档解释:Creates...

  • iOS面试题 GCD部分

    1、GCD的队列(dispatch_queue_t)分哪两种类型? 答:串行队列Serial Dispatch Q...

  • GCD 去实现NStimer

    ///GCD 去实现NSTimer///1 取得一个队列dispatch_queue_t queue = disp...

网友评论

    本文标题:GCD(二)-dispatch_queue_t dispatch

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