dispatch_sync()总是在主线程中执行块 [英] dispatch_sync() always execute block in main thread
问题描述
如果在3个不同的队列中调用dispatch_sync,如
1,那么有什么区别。
dispatch_sync(dispatch_get_main_queue(),^(void){
NSLog(@this execute in main thread)//通过[NSThread isMainThread]
} );
2。
code> dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0),^(void){
NSLog(@这也在主线程中执行)//通过[NSThread isMainThread]
}
3。
dispatch_queue_t queue;
queue = dispatch_queue_create(com.example.MyQueue,NULL);
dispatch_sync(queue,^(void){
NSLog线程)//通过[NSThread isMainThread]
}
每当我调用dispatch_sync,block在主线程中执行,而不考虑在哪个队列中被调度。因此,为什么这个函数将队列作为参数,因为它不使用它,有人可以澄清这一点吗?
dispatch_sync
是一个阻止操作。
dispatch_sync()
的线程上的块。即使在同步队列上调用块的情况下, dispatch_sync()
将要等到完成为止,在内部,它可能会停止,直到剩下的工作在队列中完成,然后直接执行块。 事实证明,将数据从线程A传递到线程B很昂贵。如果队列处于可以立即执行的状态,则 dispatch_sync
将通过简单地调用线程上的块来快速路径执行, dispatch_sync
被调用。
根据定义,你不应该关心。调用线程被阻塞 - 不能做一个事情 - 直到 dispatch_sync()
返回。
,真的,所有这是一个实现细节。 GCD可以自由地在它认为最合适的任何线程上执行块。只是发生这种情况,不上下文切换通常是最重要的规则。
Is there any difference between if dispatch_sync is called in 3 different queue like
1.
dispatch_sync(dispatch_get_main_queue(),^(void){
NSLog(@"this execute in main thread") // via [NSThread isMainThread]
});
2.
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
NSLog(@"this also execute in main thread") // via [NSThread isMainThread]
}
3.
dispatch_queue_t queue;
queue = dispatch_queue_create("com.example.MyQueue", NULL);
dispatch_sync(queue, ^(void){
NSLog(@"this also execute in main thread") // via [NSThread isMainThread]
}
Whenever i call dispatch_sync, block executed in main thread, without considering in which queue it is dispatched. So why this function take queue as a argument as it doesn't use it. Can someone please clarify this?
dispatch_sync
is a blocking operation. That is, the function will not return until the work represented in the block is completed.
When dispatched to an asynchronous queue -- like one of the global queues or a concurrent queue of your own making -- there is no reason to do anything but invoke the block on the thread that called dispatch_sync()
. Even in the case of invoking the block on a synchronous queue, the dispatch_sync()
is going to wait until completion anyway so, internally, it might as well stop until the rest of the work is done in the queue and then execute the block directly.
As it turns out, passing data from thread A to thread B is expensive. If the queue is in the state where execution can happen immediately, then dispatch_sync
will fast path the execution by simply calling the block on the thread that dispatch_sync
was called on.
And, by definition, you shouldn't care. The calling thread is blocked -- can't do a thing -- until dispatch_sync()
returns.
So, really, all of this is an implementation detail. GCD is free to execute the blocks on whatever threads it deems most appropriate. It just so happens that don't context switch is often the most important rule of figuring that out.
这篇关于dispatch_sync()总是在主线程中执行块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!