GCD全解-02-dispatch_queue-调度队列

这是一篇关于GCD的集合文章,总结了常见用法,涵盖多线程调度、与其他iOS多线程方案对比的优劣势及API使用。介绍了队列分类,包括串行队列和并行队列,还提及创建Queue的方法,后续会补充详细内容和案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GCD系列是阅读官方文件和在实践中总结的一些常见的GCD用法,基本涉及全部的GCD内容形成的集合文章,文章重点在与精简和全面覆盖。

学习本集合你可以了解:
1. GCD是如何做到多线程调度的
2. 对比其他iOS的多线程方案,GCD的优势和劣势
3. 如何使用GCD相关的API

我将在后续不断补充详细内容和实际案例, 欢迎关注,提问和讨论

01-dispatch-iOS系统调度

02-dispatch_queue-调度队列

03-dispatch_after/dispatch_time-延迟调度和操作

04-dispatch_barrier_sync/async-线程阻塞

05-dispatch_apply-重复提交操作

06-dispatch_once-单次执行

07-dispatch_semaphore-信号量/数据同步

08-dispatch_group-调度组/多异步操作控制

09-dispatch_block-GCD取消操作

10-dispatch_source-调度资源

11-GCD死锁

队列分类

串行队列 :

DISPATCH_QUEUE_SERIAL

@discussion A dispatch queue that invokes blocks serially in FIFO order.
#define DISPATCH_QUEUE_SERIAL NULL

dispatch_get_main_queue();

@discussion Returns the default queue that is bound to the main thread.

dispatch_queue_t dispatch_get_main_queue(void){
    return DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q);
}

并行队列:

DISPATCH_QUEUE_CONCURRENT

@discussion A dispatch queue that may invoke blocks concurrently and supports barrier blocks submitted with the dispatch barrier API.

#define DISPATCH_QUEUE_CONCURRENT \
        DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, _dispatch_queue_attr_concurrent)

dispatch_get_global_queue(queue_priority, 0);

@discussion Returns a well-known global concurrent queue of a given quality of service class.The well-known global concurrent queues may not be modified. 

@param identifier
A quality of service class defined in qos_class_t or a priority defined in dispatch_queue_priority_t.

@param flags
Reserved for future use. 默认传入0就好了

dispatch_queue_t dispatch_get_global_queue(long identifier, unsigned long flags);

quality of service & queue_priority

The global concurrent queues are identified by their priority, which map to the following QOS classes:
DISPATCH_QUEUE_PRIORITY_HIGH:         QOS_CLASS_USER_INITIATED
DISPATCH_QUEUE_PRIORITY_DEFAULT:      QOS_CLASS_DEFAULT
DISPATCH_QUEUE_PRIORITY_LOW:          QOS_CLASS_UTILITY
DISPATCH_QUEUE_PRIORITY_BACKGROUND:   QOS_CLASS_BACKGROUND

创建Queue

dispatch_queue_create(@”描述字符串”,DISPATCH_QUEUE_CONCURRENT / DISPATCH_QUEUE_SERIAL )

* Creates a new dispatch queue to which blocks may be submitted.
 * @discussion
    传入DISPATCH_QUEUE_SERIAL or a NULL创建的是顺序执行的串行队列

    传入DISPATCH_QUEUE_CONCURRENT创建的是并发队(有点像是global concurrent queues)

    Block会引用Queue,当Block执行完只有Queue才会被释放
 * When a dispatch queue is no longer needed, it should be released with
 * dispatch_release(). Note that any pending blocks submitted to a queue will
 * hold a reference to that queue. Therefore a queue will not be deallocated
 * until all pending blocks have finished.

 * @param label
     A string label to attach to the queue. This parameter is optional and may be NULL.
 * @param attr
     A predefined attribute such as DISPATCH_QUEUE_SERIAL, DISPATCH_QUEUE_CONCURRENT.

dispatch_queue_t dispatch_queue_create(const char *_Nullable label, dispatch_queue_attr_t _Nullable attr);
//获取主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();

//串行队列
DISPATCH_QUEUE_SERIAL == NULL
dispatch_queue_t queue2 = dispatch_queue_create("queue2", DISPATCH_QUEUE_SERIAL);

//并行队列
dispatch_queue_t queue3 = dispatch_queue_create("queue3", DISPATCH_QUEUE_CONCURRENT);

//全局并发队列
dispatch_queue_t queue4 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值