是dispatch_sync(dispatch_get_global_queue(xxx),任务)同步或异步 [英] Is dispatch_sync(dispatch_get_global_queue(xxx), task) sync or async

查看:253
本文介绍了是dispatch_sync(dispatch_get_global_queue(xxx),任务)同步或异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如Apple的文档所说,dispatch_get_global_queue()是一个并发队列,dispatch_sync是一个意思是serial.thes任务被处理为异步或同步?

As Apple's document says, dispatch_get_global_queue() is a concurrent queue, and dispatch_sync is something meaning serial.Then the tasks are processed async or sync?

推荐答案

队列是什么和异步与同步意味着什么之间的混淆。

You're getting confused between what a queue is and what async vs sync means.

队列是一个实体, 。这些可以是串行或并发的。串行意味着如果你按顺序A,B,C,D放置块,则它们将被执行A,然后B,然后C,然后D.并行意味着这些相同的块可能以不同的顺序执行,并且可能甚至多个同时(假设你有多个核心运行,显然)。

A queue is an entity on which blocks can be run. These can be serial or concurrent. Serial means that if you put block on in the order A, B, C, D, then they will be executed A, then B, then C, then D. Concurrent means that these same blocks might be executed in a different order and possibly even more than one at the same time (assuming you have multiple cores on which to run, obviously).

然后到异步vs同步。 Async意味着当调用 dispatch_async 时,它会立即返回 ,该块将在队列中排队。同步意味着当你调用 dispatch_sync 时,它只会在块完成执行后返回。

Then onto async vs sync. Async means that when you call dispatch_async, it will return immediately and the block will be queued on the queue. Sync means that when you call dispatch_sync it will return only after the block has finished executing.

你的问题,如果你 dispatch_sync 到全局并行队列,那么这个块将被运行,也许与该队列上的其他块并行,但是以同步的方式 - 即它赢了不返回,直到块完成。

So to fully answer your question, if you dispatch_sync onto a global concurrent queue then this block will be run, perhaps in parallel with other blocks on that queue, but in a synchronous manner - i.e. it won't return until the block is finished.

这篇关于是dispatch_sync(dispatch_get_global_queue(xxx),任务)同步或异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆