队列queue.h方法总览
创建队列(queue)相关方法:
/**
获取当前队列 macos(10.6,10.9), ios(4.0,6.0))
*/
dispatch_queue_t
dispatch_get_current_queue(void);
/**
获取主队列(串行)macos(10.6), ios(4.0)
*/
dispatch_queue_t
dispatch_get_main_queue(void);
/**
全局队列 :本质就是并发队列;不可跟踪;在MRC中释放次数不一样 macos(10.6), ios(4.0)
@param identifier 优先级
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
@param flags 预留(无用)一般为0
*/
dispatch_queue_t
dispatch_get_global_queue(long identifier, unsigned long flags);
/**
根据已知队列创建队列 macos(10.12), ios(10.0), tvos(10.0), watchos(3.0)
@param label 标签 eg: @"com.Maker"
@param attr 属性(attribute)可以通过dipatch_queue_attr_make_with_qos_class或
dispatch_set_target_queue方法设置 dispatch_queue_attr_t
@param target 目标队列,相当于目标队列计数 加一
*/
dispatch_queue_t
dispatch_queue_create_with_target(const char *_Nullable label,
dispatch_queue_attr_t _Nullable attr, dispatch_queue_t _Nullable target)
DISPATCH_ALIAS_V2(dispatch_queue_create_with_target);
/**
创建队列(串行 / 并发) macos(10.6), ios(4.0)
@param label 标签 eg: @"com.Maker"
@param attr 属性(attribute)可以通过dipatch_queue_attr_make_with_qos_class或
dispatch_set_target_queue方法设置 dispatch_queue_attr_t
*/
dispatch_queue_t
dispatch_queue_create(const char *_Nullable label,
dispatch_queue_attr_t _Nullable attr);
举例说明:
//当前队列
dispatch_queue_t currentQueue = dispatch_get_current_queue();
dispatch_async(currentQueue, ^(){
for (int i = 0; i < 100; i++) {
NSLog(@"任务_1====%d",[NSThread currentThread].isMainThread);
}
});
//主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^(){
for (int i = 0; i < 100; i++) {
NSLog(@"任务_1====%d",[NSThread currentThread].isMainThread);
}
});
//全局队列
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
dispatch_async(globalQueue, ^(){
for (int i = 0; i < 100; i++) {
NSLog(@"任务_1====%d",[NSThread currentThread].isMainThread);
}
});
//target队列
dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT, QOS_CLASS_BACKGROUND, -1);
dispatch_queue_t queue = dispatch_queue_create("com.Maker", attr);
dispatch_queue_t targetQueue = dispatch_queue_create_with_target("com.Maker", attr, queue);
dispatch_async(targetQueue, ^(){
for (int i = 0; i < 10; i++) {
NSLog(@"任务_1====%d",[NSThread currentThread].isMainThread);
}
});
dispatch_async(targetQueue, ^(){
for (int i = 0; i < 10; i++) {
NSLog(@"任务_2====%d",[NSThread currentThread].isMainThread);
}
});
//创建队列
dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, -1);
dispatch_queue_t globalQueue = dispatch_queue_create("com.Maker", attr);
dispatch_async(globalQueue, ^(){
for (int i = 0; i < 100; i++) {
NSLog(@"任务_1====%d",[NSThread currentThread].isMainThread);
}
});