gpt4 book ai didi

ios - 分派(dispatch)到并发队列导致在主线程上执行?

转载 作者:行者123 更新时间:2023-11-29 12:39:57 27 4
gpt4 key购买 nike

Apple 的 GCD 文档说明如下:

GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects. Blocks submitted to dispatch queues are executed on a pool of threads fully managed by the system. No guarantee is made as to the thread on which a task executes. GCD offers three kinds of queues:

这是否意味着即使我发出诸如

之类的请求
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{...});

它会导致代码块在主线程上执行吗?在那种情况下,似乎在主线程上使用并发队列调用 dispatch_sync 可能会导致主线程卡在等待自身的死锁情况。

我对 GCD 文档的解释是否正确?

最佳答案

是的,如果调用者在主线程上,似乎 dispatch_sync 到全局队列可能意味着在主线程上执行代码。 dispatch_sync 的文档说明:

As an optimization, this function invokes the block on the current thread when possible.

dispatch_sync always scheduling a block on Main Thread

关于ios - 分派(dispatch)到并发队列导致在主线程上执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25255362/

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