美文网首页
Concurrency Programming Guide 笔记

Concurrency Programming Guide 笔记

作者: V_coa | 来源:发表于2016-06-04 10:53 被阅读16次

Queue-Related Technologies

* Dispatch groups 它通过监听block有没有执行完成,可以监听同步或者异步
* Dispatch semaphores 它和传统的信号量类似
* Dispatch sources 它生成通知,并响应系统的事件,可以用它监听事件

Performing Loop Iterations Concurrently

如果你的每次循环是互不相关的,可以通过dispatch_apple或dispatch_apply_f,通过传入concurrent queue,它们能同时执行多个循环
dispatch_apply(100,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t i) {
        NSLog(@"%@", @(i));
    });

Using Dispatch Semaphores to Regulate the Use of Finite Resources

这个就类似于操作系统的信号量。用来管理有限的资源。
* 用 dispatch_semaphore_create 生成一个信号量,参数是一个资源的可用数量
* 在每个任务中,调用 dispatch_semaphore_wait 等待信号量
* 当上面wait return,获取资源并继续执行
* 当你完成资源,释放它,并调用 dispatch_semaphore_signal

相关文章

网友评论

      本文标题:Concurrency Programming Guide 笔记

      本文链接:https://www.haomeiwen.com/subject/hayldttx.html