版权声明:知识版权是属于全人类的! 欢迎评论与转载!!! https://blog.csdn.net/zhuge1127/article/details/82222977
队列分类
串行队列 :
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);