种类
|
说明
|
Serial Dispatch Queue
|
等待现在执行中处理结束(顺序执行)
|
Concurrent Dispatch Queue
|
不等待现在执行中处理结束(并发执行)
|
dispatch_queue_t
myQu=dispatch_queue_create("log", DISPATCH_QUEUE_CONCURRENT);
|
dispatch_queue_t myQU=dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myQU, ^{ NSLog(@"test"); });
dispatch_release(myQU);
|
名称
|
种类
|
说明
|
Main Dispatch Queue
|
Serial Dispatch Queue
|
主线程执行
|
Global Dispatch Queue
|
Concurrent Dispatch Queue |
有四个等级:High、Default、Low、Background。分别对应高、默认、低、后台
|
名称
|
获取方法
|
Main Dispatch Queue |
dispatch_queue_t
mainQU=dispatch_get_main_queue(); |
Global Dispatch Queue |
dispatch_queue_t
globalQU=dispatch_get_global_queue(<#dispatch_queue_priority_t priority#>, 0);
第一个参数代表等级,分别有:
#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
第二个参数是保留参数,直接写0吧。
|
dispatch_async
(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
/** * 并行执行的处理 */ //需要在主线程展处理的,例如界面更新 dispatch_async(dispatch_get_main_queue(), ^{ /** * 主线程处理 */ });
});
|
dispatch_queue_t mySerialQueue=dispatch_queue_create("test_serial", NULL);
dispatch_queue_t globalBackground=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_set_target_queue(mySerialQueue, globalBackground);
|
函数:
void
dispatch_after(dispatch_time_t when,
dispatch_queue_t
queue,dispatch_block_t block);
使用:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@“after");
});
dispatch_time_t
dispatch_time(dispatch_time_t when, int64_t delta);
dispatch_time_t
dispatch_walltime(const struct timespec *when, int64_t delta);
timespec结构体可以通过NSDate类型的数据生成。
|
dispatch_queue_t globalQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group=dispatch_group_create();
dispatch_group_async(group, globalQueue, ^{NSLog(@"0");});
dispatch_group_async(group, globalQueue, ^{NSLog(@"1");}); dispatch_group_async(group, globalQueue, ^{NSLog(@"2");});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{NSLog(@"done");});
|
dispatch_queue_t globalQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group=dispatch_group_create();
dispatch_group_async(group, globalQueue, ^{NSLog(@“0");});
dispatch_group_async(group, globalQueue, ^{NSLog(@"1");});
dispatch_group_async(group, globalQueue, ^{NSLog(@"2");});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
|
dispatch_queue_t concurrentQU=dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQU, ^{
NSLog(@"write data0");});
dispatch_async(concurrentQU, ^{ NSLog(@"write data1");});
dispatch_async(concurrentQU, ^{ NSLog(@"write data2"); });
dispatch_async(concurrentQU, ^{ NSLog(@"write data3");});
dispatch_barrier_async(concurrentQU, ^{
NSLog(@"read...");
});
dispatch_async(concurrentQU, ^{ NSLog(@"write data4”); });
dispatch_async(concurrentQU, ^{ NSLog(@"write data5”); });
执行结果是:
write data1
write data0
write data2
write data3
read...
write data4
write data5
|
Submits a barrier block for asynchronous execution and returns immediately. DeclarationObjective-C
Parameters
DiscussionCalls to this function always return immediately after the block has been submitted and never wait for the block to be invoked. When the barrier block reaches the front of a private concurrent queue, it is not executed immediately. Instead, the queue waits until its currently executing blocks finish executing. At that point, the barrier block executes by itself. Any blocks submitted after the barrier block are not executed until the barrier block completes. The queue you specify should be a concurrent queue that you create yourself using the |
dispatch_barrier_sync,此函数与
dispatch_barrier_async函数的区别是它会等待它所提交的Block执行完毕才返回,也就是说会阻塞线程,等Block执行完毕后函数后面的任务才会被提交到进程。个人理解是,
dispatch_barrier_async提交任务后函数之后的任务也被提交,但并未执行,等函数提交的任务执行后,在其之后提交的任务才执行,而dispatch_barrier_sync函数提交任务后知道任务执行结束才接着往下执行。
dispatch_barrier_async使用率较高。
官网说明如下:
Submits a barrier block object for execution and waits until that block completes. DeclarationObjective-C
Parameters
DiscussionSubmits a barrier block to a dispatch queue for synchronous execution. Unlike When the barrier block reaches the front of a private concurrent queue, it is not executed immediately. Instead, the queue waits until its currently executing blocks finish executing. At that point, the queue executes the barrier block by itself. Any blocks submitted after the barrier block are not executed until the barrier block completes. The queue you specify should be a concurrent queue that you create yourself using the Unlike with As an optimization, this function invokes the barrier block on the current thread when possible. |
dispatch_queue_t queue=dispatch_get_main_queue();
dispatch_sync(queue, ^{ NSLog(@"hello"); });
该源码在主线程中执行指定的Block,并等待其执行结束,然而主线程正在执行源码,已经被阻塞,无法继续执行任何源码。
|
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply(5, queue, ^(size_t index) { NSLog(@"%zu",index); });
NSLog(@"done");
运行结果:1 0 3 2 4 done
|
dispatch_semaphore_t semaphore=dispatch_semaphore_create(1);//参数表示计数的初始值
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//此函数等待计数值等于或大于1,对该技术进行减法并返回,第二个参数是等待时间,若在等待时间内semaphore的计数满足要求,则返回0,反之返回非0;
dispatch_semaphore_signal(semaphore);//
该函数使
semaphore
加
1
;
|
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//生成计数信号(信号量),初始值为1,保证同时访问数组的线程只有一个。 dispatch_semaphore_t sema=dispatch_semaphore_create(1); NSMutableArray*array=[[NSMutableArray alloc]init]; for (int i=0; i<10000; i++) { dispatch_async(queue, ^{ //等待计数信号直到大于等于1 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); /** * 由于第一个访问的线程访问时,信号量等于1,满足条件,wait函数返回,信号量为0,其余访问的线程只能等待,能访问array的线程始终只有一个 */ [array addObject:[NSNumber numberWithInt:i]]; //排他控制结束,信号量加1 dispatch_semaphore_signal(sema); });
}
|
static dispatch_once_t pred;
dispatch_once(&pred, ^{
/** * 初始化代码 */
});
|
dispatch_async(queue, ^{/*读取0~8191字节*/});
dispatch_async(queue, ^{/*读取8192~16383字节*/});
dispatch_async(queue, ^{/*
读取
16383~24575
字节
*/
});
|
pipe_q = dispatch_queue_create("PipeQ", NULL);
pipe_channel = dispatch_io_create(DISPATCH_IO_STREAM, fd, pipe_q, ^(int err){
close(fd);
});
*out_fd = fdpair[1];
dispatch_io_set_low_water(pipe_channel, SIZE_MAX);
dispatch_io_read(pipe_channel, 0, SIZE_MAX, pipe_q, ^(bool done, dispatch_data_t pipedata, int err){
if (err == 0)
{
size_t len = dispatch_data_get_size(pipedata);
if (len > 0)
{
const char *bytes = NULL;
char *encoded;
dispatch_data_t md = dispatch_data_create_map(pipedata, (const void **)&bytes, &len);
encoded = asl_core_encode_buffer(bytes, len);
asl_set((aslmsg)merged_msg, ASL_KEY_AUX_DATA, encoded);
free(encoded);
_asl_send_message(NULL, merged_msg, -1, NULL);
asl_msg_release(merged_msg);
dispatch_release(md);
}
}
if (done)
{
dispatch_semaphore_signal(sem);
dispatch_release(pipe_channel);
dispatch_release(pipe_q);
}
});
|