gpt4 book ai didi

grand-central-dispatch - GCD 队列类型

转载 作者:行者123 更新时间:2023-12-02 02:14:32 27 4
gpt4 key购买 nike

我正在尝试了解队列类型之间的差异。据我了解,有 3 种类型:

  • 全局队列 - 并发 - block 无论顺序如何都会尽快执行
  • 主队列 - 串行 - block 在提交时执行
  • 私有(private)队列 - 串行

我想知道的是:dispatch_sync 和 dispatch_async 在提交到各种队列时有什么区别?到目前为止,这是我的理解:

dispatch_sync(global_queue)^
{
// blocks are executed one after the other in no particular order
// example: block 3 executes. when it finishes block 7 executes.
}

dispatch_async(global_queue)^
{
// blocks are executed concurrently in no particular order
// example: blocks 2,4,5,7 execute at the same time.
}

dispatch_sync(main_queue)^
{
// blocks are executed one after the other in the order they were submitted
// example: block 1 executes. when it finishes block 2 will execute and so forth.
}

dispatch_async(main_queue)^
{
// blocks are executed concurrently in the order they were submitted
// example: blocks 1-4 (or whatever amount of threads the system can handle at one time) will fire at the same time.
// when the first block completes block 5 will then execute.
}

我想知道我对此的看法在多大程度上是正确的。

最佳答案

dispatch_sync 和 dispatch_async 之间的区别有点像 windows API 中的 sendmessage 和 postmessage。调度同步提交一个 block 对象以在调度队列上执行并等待该 block 完成。dispatch_async在调度队列上提交异步执行 block 并立即返回。

目标队列确定该 block 是串行调用还是与提交到同一队列的其他 block 同时调用。独立的串行队列相互并发处理。

在发布问题之前,您应该先仔细阅读文档。

关于grand-central-dispatch - GCD 队列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11209884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com