在提出一些概念前,我们先来看两段代码
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"1111");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"2222");
});
}
问题:输出是什么?
第二个:
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue1 = dispatch_queue_create("com.apple.queue1", DISPATCH_QUEUE_SERIAL);
NSLog(@"start");
dispatch_async(queue1, ^{
sleep(5);
NSLog(@"1 -- %@",[NSThread currentThread]);
});
dispatch_sync(queue1, ^{
sleep(5);
NSLog(@"2 -- %@",[NSThread currentThread]);
});
sleep(5);
NSLog(@"finished");
}
问题:耗时多长?
dispatch_queue_attr_t
定义如下:
DISPATCH_DECL(dispatch_queue_attr);
#define DISPATCH_DECL(name) typedef struct name##_s *name##_t;
#define DISPATCH_VTABLE_HEADER(x) \
unsigned long const do_type; \
const char *const do_kind; \
size_t (*const do_debug)(struct x *, char *, size_t); \
struct dispatch_queue_s *(*const do_invoke)(struct x *); \
bool (*const do_probe)(struct x *); \
void (*const do_dispose)(struct x *)
#define DISPATCH_STRUCT_HEADER(x, y) \
const struct y *do_vtable; \
struct x *volatile do_next; \
unsigned int do_ref_cnt; \
unsigned int do_xref_cnt; \
unsigned int do_suspend_cnt; \
struct dispatch_queue_s *do_targetq; \
void *do_ctxt; \
void *do_finalizer
#define DISPATCH_QUEUE_MIN_LABEL_SIZE 64
#ifdef __LP64__
#define DISPATCH_QUEUE_CACHELINE_PAD 32
#else
#define DISPATCH_QUEUE_CACHELINE_PAD 8
#endif
struct dispatch_object_vtable_s {
DISPATCH_VTABLE_HEADER(dispatch_object_s);
};
//将宏展开
struct dispatch_object_vtable_s {
unsigned long const do_type;
const char *const do_kind;
size_t (*const do_debug)(struct dispatch_object_s *, char *, size_t);
struct dispatch_queue_s *(*const do_invoke)(struct x *);
bool (*const do_probe)(struct dispatch_object_s *);
void (*const do_dispose)(struct dispatch_object_s *)
};
struct dispatch_object_s {
DISPATCH_STRUCT_HEADER(dispatch_object_s, dispatch_object_vtable_s);
};
//将宏展开
struct dispatch_object_s {
const struct dispatch_object_vtable_s *do_vtable;
struct dispatch_object_s *volatile do_next;
unsigned int do_ref_cnt;
unsigned int do_xref_cnt;
unsigned int do_suspend_cnt;
struct dispatch_queue_s *do_targetq;
void *do_ctxt;
void *do_finalizer;
};
#define DISPATCH_QUEUE_HEADER \
uint32_t volatile dq_running; \
uint32_t dq_width; \
struct dispatch_object_s *volatile dq_items_tail; \
struct dispatch_object_s *volatile dq_items_head; \
unsigned long dq_serialnum; \
dispatch_queue_t dq_specific_q;
struct dispatch_queue_s {
DISPATCH_STRUCT_HEADER(dispatch_queue_s, dispatch_queue_vtable_s);
DISPATCH_QUEUE_HEADER;
char dq_label[DISPATCH_QUEUE_MIN_LABEL_SIZE]; // must be last
char _dq_pad[DISPATCH_QUEUE_CACHELINE_PAD]; // for static queues only
};
//将宏展开
struct dispatch_queue_s {
const struct dispatch_queue_vtable_s *do_vtable;
struct dispatch_queue_s *volatile do_next;
unsigned int do_ref_cnt;
unsigned int do_xref_cnt;
unsigned int do_suspend_cnt;
struct dispatch_queue_s *do_targetq;
void *do_ctxt;
void *do_finalizer;
uint32_t volatile dq_running;
uint32_t dq_width;
struct dispatch_object_s *volatile dq_items_tail;
struct dispatch_object_s *volatile dq_items_head;
unsigned long dq_serialnum;
dispatch_queue_t dq_specific_q;
char dq_label[DISPATCH_QUEUE_MIN_LABEL_SIZE]; // must be last
char _dq_pad[DISPATCH_QUEUE_CACHELINE_PAD]; // for static queues only
};
struct dispatch_queue_attr_vtable_s {
DISPATCH_VTABLE_HEADER(dispatch_queue_attr_s);
};
struct dispatch_queue_attr_s {
DISPATCH_STRUCT_HEADER(dispatch_queue_attr_s, dispatch_queue_attr_vtable_s);
// Public:
int qa_priority;
void* finalizer_ctxt;
dispatch_queue_finalizer_function_t finalizer_func;
// Private:
unsigned long qa_flags;
};
//宏影响阅读,将其展开
struct dispatch_queue_attr_vtable_s {
unsigned long const do_type;
const char *const do_kind;
size_t (*const do_debug)(struct dispatch_queue_s *, char *, size_t);
struct dispatch_queue_s *(*const do_invoke)(struct dispatch_queue_s *);
bool (*const do_probe)(struct dispatch_queue_s *);
void (*const do_dispose)(struct dispatch_queue_s *);
};
struct dispatch_queue_attr_s {
const struct dispatch_queue_attr_vtable_s *do_vtable;
struct dispatch_queue_attr_s *volatile do_next;
unsigned int do_ref_cnt;
unsigned int do_xref_cnt;
unsigned int do_suspend_cnt;
struct dispatch_queue_s *do_targetq;
void *do_ctxt;
void *do_finalizer;
// Public:
int qa_priority;
void* finalizer_ctxt;
dispatch_queue_finalizer_function_t finalizer_func;
// Private:
unsigned long qa_flags;
};
参考:
[1] : 深入理解 GCD
[1] : [Objective-C]深入理解GCD
[1] : https://opensource.apple.com/tarballs/libdispatch/
[1] : 探秘Runtime - Runtime源码分析
[1] : GCD源码分析