dispatch_once中的块在哪个线程中运行? [英] Which thread does the block in dispatch_once run in?

查看:89
本文介绍了dispatch_once中的块在哪个线程中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dispatch_once中的块在哪个线程中运行? 如果代码是从主线程运行的,则能否在后台线程中运行dispatch_once块?如何确保它在主线程上运行,而不管哪个线程执行它?

Which thread does the block in dispatch_once run in? Can dispatch_once block run in background thread if the code is run from main thread? How do I ensure it runs on the main thread regardless of which thread executes it?

推荐答案

它在当前/调用线程中运行.如果您想,我想您可以使用dispatch_sync来确保它在后台线程上运行,但是我不确定会得到什么.总之,它在当前线程中运行.如果dispatch_once块中已经有另一个线程,则调用线程将一直阻塞,直到该块在另一个线程上执行完毕,然后继续.

It runs in the current/calling thread. If you wanted I suppose you could use dispatch_sync to ensure it runs on a background thread but I'm not sure what that'd get you. In sum, it runs in the current thread. If another thread is already in the dispatch_once block, the calling thread will block until the block finishes executing on the other thread, and then proceed.

这篇关于dispatch_once中的块在哪个线程中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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