如果dispatch_sync不使用其他线程,可以更快吗? [英] If dispatch_sync does not use a different thread, can it be faster?
本文介绍了如果dispatch_sync不使用其他线程,可以更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行这段代码
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"Main Thread? %d", [NSThread isMainThread]);
// non-trivial non-UI code here
});
NSLog
表示我实际上在主线程上,但是...我的印象是,如果我使用 dispatch_sync <,那么平凡的非UI代码的运行速度会更快/code>(而不是什么).
The NSLog
indicates that I'm actually on the main thread, but... my impression is that the non-trivial non-UI code runs much faster if I use dispatch_sync
(as opposed to nothing).
如果它在主线程上运行,有什么办法可以更快?
推荐答案
全局并发GCD队列没有排序,将其中一个传递给dispatch_sync()是noop,而dispatch_sync()只会在此直接执行指定的块情况,就好像您写过
The global concurrent GCD queues have no ordering, passing one of them to dispatch_sync() is a noop and dispatch_sync() will just execute the specified block directly in this case, as if you had written
^{...}();
这篇关于如果dispatch_sync不使用其他线程,可以更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文