dispatch_sync() 总是在主线程中执行块 [英] dispatch_sync() always execute block in main thread

查看:29
本文介绍了dispatch_sync() 总是在主线程中执行块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在 3 个不同的队列中调用 dispatch_sync 有什么区别

1.

 dispatch_sync(dispatch_get_main_queue(),^(void){NSLog(@"this 在主线程中执行")//通过 [NSThread isMainThread]});

2.

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){NSLog(@"这也在主线程中执行")//通过 [NSThread isMainThread]}

3.

dispatch_queue_t 队列;queue = dispatch_queue_create("com.example.MyQueue", NULL);dispatch_sync(队列,^(无效){NSLog(@"这也在主线程中执行")//通过 [NSThread isMainThread]}

每当我调用 dispatch_sync 时,都会阻塞在主线程中执行,而不考虑在哪个队列中调度它.那么为什么这个函数将队列作为参数,因为它不使用它.有人可以澄清一下吗?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆