Objective C 高级进阶 — GCD队列终结篇

鉴于GCD队列的方法太多了,这里没办法一一讲解,了解了其中几个的运行机制之后,其他的都可以随时拿来用,主要是理解其运行机制

这里再给大家讲解一下dispatch_get_global_queue 与 dispatch_get_main_queue的区别,先来看看官方原文

dispatch_get_global_queue
Returns a well-known global concurrent queue of a given priority level.

dispatch_queue_t dispatch_get_global_queue(
   long priority,
   unsigned long flags);
Parameters
priority
The priority of the queue being retrieved. For a list of possible values, see “dispatch_queue_priority_t”.
flags
This value is reserved for future use. You should always pass 0.
Return Value
Returns the requested global queue.

Discussion
The well-known global concurrent queues cannot be modified. Calls to dispatch_suspend, dispatch_resume, dispatch_set_context, and the like have no effect when used with queues returned by this function.

Blocks submitted to these global concurrent queues may be executed concurrently with respect to each other.

Availability
Available in iOS 4.0 and later.
Declared In
dispatch/queue.h

global_queue,实际上是创建了一个全局的并行队列,并且可以设置队列的优先级,这个队列一旦设置之后就不可修改,并且不能suspend, resume, set_context.

来看代码运行图

可以看到,向global队列添加的block不是在主线程中运行的。对于验证global队列的并行模式,大家可以参考上篇文章中提到的方式去验证,这里就不做验证了。

下边来继续看main_queue


dispatch_get_main_queue
Returns the serial dispatch queue associated with the application’s main thread.

dispatch_queue_t dispatch_get_main_queue(void);
Return Value
Returns the main queue. This queue is created automatically on behalf of the main thread before main is called.

Discussion
The main queue is automatically created by the system and associated with your application’s main thread. Your application uses one (and only one) of the following three approaches to invoke blocks submitted to the main queue:

Calling dispatch_main
Calling UIApplicationMain (iOS) or NSApplicationMain (OS X)
Using a CFRunLoopRef on the main thread
As with the global concurrent queues, calls to dispatch_suspend, dispatch_resume, dispatch_set_context, and the like have no effect when used with queues returned by this function.

Availability
Available in iOS 4.0 and later.
Declared In
dispatch/queue.h

main_queue 是返回一个与application 主线程相关的串行队列。

来看代码运行图:


向main_queue提交的block是运行在主线程中的,对于串行的验证,大家也是根据上篇提到的去验证吧。

后续如果有新的发现,我会再整理的,目前就写到这里吧,如有问题,请及时提出



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值