博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Waiting on Groups of Queued Tasks
阅读量:6940 次
发布时间:2019-06-27

本文共 1747 字,大约阅读时间需要 5 分钟。

https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW25

Waiting on Groups of Queued Tasks

Dispatch groups are a way to block a thread until one or more tasks finish executing. You can use this behavior in places where you cannot make progress until all of the specified tasks are complete. For example, after dispatching several tasks to compute some data, you might use a group to wait on those tasks and then process the results when they are done. Another way to use dispatch groups is as an alternative to thread joins. Instead of starting several child threads and then joining with each of them, you could add the corresponding tasks to a dispatch group and wait on the entire group. 

Listing 3-6 shows the basic process for setting up a group, dispatching tasks to it, and waiting on the results. Instead of dispatching tasks to a queue using the  function, you use the  function instead. This function associates the task with the group and queues it for execution. To wait on a group of tasks to finish, you then use the  function, passing in the appropriate group.

Listing 3-6  Waiting on asynchronous tasks

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
 
// Add a task to the group
dispatch_group_async(group, queue, ^{
// Some asynchronous work
});
 
// Do some other work while the tasks execute.
 
// When you cannot make any more forward progress,
// wait on the group to block the current thread.
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
 
// Release the group when it is no longer needed.
dispatch_release(group);

转载地址:http://alfnl.baihongyu.com/

你可能感兴趣的文章
获取元素offset的方法
查看>>
Javascript综合应用小案例
查看>>
想统计指定用户每天通过EXCHANGE发送了多少封邮件么?
查看>>
PostgreSQL中,如何查询表所对应的文件名
查看>>
循环、迭代、遍历和递归
查看>>
chrome操作技巧
查看>>
cocos2d-x之qt port
查看>>
HDU-4568 Hunter 状态压缩
查看>>
使用MyEclipse开发第一个Web程序
查看>>
Windows 7上的DirectX 11.1
查看>>
屌丝程序员的那些事(二)-第一次面试
查看>>
JSP基础(二)JSP语法概述
查看>>
京东商城招聘自动调价系统架构师 T4级别
查看>>
浅C#中的装箱和拆箱
查看>>
JavaScript富应用MVC MVVM框架
查看>>
采用左右值编码来存储无限分级树形结构的数据库表设计
查看>>
[置顶] 信息熵的计算
查看>>
WinJS.Binding.List与kendo.data.ObservableArray
查看>>
Windows环境下32位汇编语言程序设计(典藏版)
查看>>
Fedora 19的U盘安装 以及简单配置
查看>>