gpt4 book ai didi

ios - dispatch_sync 死锁但不在该队列上

转载 作者:行者123 更新时间:2023-11-28 20:17:03 26 4
gpt4 key购买 nike

此代码阻塞了对 dispatch_sync 的调用。我是调度队列的新手。这会阻止什么原因?

NSLog(@"%@",dispatch_get_current_queue());
NSLog(@"%@",dispatch_get_main_queue());

if (dispatch_get_current_queue() == dispatch_get_main_queue())
{
block();
}
else
dispatch_sync(dispatch_get_main_queue(),block);

日志打印这些队列

OS_dispatch_queue_root: com.apple.root.low-priority[0x345bbc0]

OS_dispatch_queue:com.apple.main-thread[0x345b900]>

最佳答案

将当前队列与主队列进行比较并不是检查您是否在主线程上运行的有效方法。

来自 GCD 文档 - http://developer.apple.com/library/mac/#DOCUMENTATION/Darwin/Reference/ManPages/man3/dispatch_get_main_queue.3.html

The result of dispatch_get_main_queue() may or may not equal the result of dispatch_get_current_queue() when called on the main thread. Comparing the two is not a valid way to test whether code is executing on the main thread. Foundation/AppKit programs should use [NSThread isMainThread]. POSIX programs may use pthread_main_np(3).

dispatch_get_current_queue() 已弃用,它仅用于调试,因此您不应在生产代码中使用它。

不需要检查这个队列是否是主队列,只需在主队列上对其进行 dispatch_sync,它就会到达那里。

关于ios - dispatch_sync 死锁但不在该队列上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17679513/

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