GCD系列是阅读官方文件和在实践中总结的一些常见的GCD用法,基本涉及全部的GCD内容形成的集合文章,文章重点在与精简和全面覆盖。
学习本集合你可以了解:
1. GCD是如何做到多线程调度的
2. 对比其他iOS的多线程方案,GCD的优势和劣势
3. 如何使用GCD相关的API我将在后续不断补充详细内容和实际案例, 欢迎关注,提问和讨论
03-dispatch_after/dispatch_time-延迟调度和操作
04-dispatch_barrier_sync/async-线程阻塞
重复执行某个任务,但是注意这个方法没有办法异步执行
(为了不阻塞线程可以使用dispatch_async()包装一下再执行
* @abstract
* Submits a block to a dispatch queue for parallel invocation.
# 提交一个Block到队列并发调用
* @discussion
* Submits a block to a dispatch queue for parallel invocation. This function
* waits for the task block to complete before returning. If the specified queue
* is concurrent, the block may be invoked concurrently, and it must therefore
* be reentrant safe.
*
* Each invocation of the block will be passed the current index of iteration.
*
* @param iterations The number of iterations(反复) to perform.
*
* @param queue
* The dispatch queue to which the block is submitted.
* The preferred value to pass is DISPATCH_APPLY_AUTO to automatically use
* a queue appropriate for the calling thread.
void dispatch_apply(size_t iterations, dispatch_queue_t queue, DISPATCH_NOESCAPE void (^block)(size_t));
demo
dispatch_apply(3, dispatch_queue_create("d", NULL), ^(size_t index) {
NSLog(@"copy-%ld", index);
});
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-0
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-1
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-2
dispatch_apply(3, dispatch_queue_create("c", DISPATCH_QUEUE_CONCURRENT), ^(size_t index) {
NSLog(@"copy-%ld", index);
});
2016-11-05 17:04:10.798 Multithreading[16153:483197] copy-2
2016-11-05 17:04:10.798 Multithreading[16153:483181] copy-1
2016-11-05 17:04:10.798 Multithreading[16153:483141] copy-0