简体   繁体   中英

Dispatch_sync from the main thread using the main thread's queue useless?

I just want to confirm my reasoning that doing a dispatch_sync from the UI thread(main thread) using the main queue is utterly useless.

To add to that, async would be just as useless but with the illusion that it is useful because it doesn't block.

Let me know.

Thanks

The dispatch_sync to the same queue is not only useless, but it will lock your app. dispatch_sync says "dispatch something, but don't proceed on this queue until the other queue responds". That obviously can't happen if the "other" queue (that we're waiting for it to complete the dispatched block) is the same one as "this" queue (which is blocked until that other queue responds). Your app will freeze, waiting for itself!

On the other hand, dispatch_async to the same queue you're currently on is not generally a very useful construction, but at least it won't freeze. I've seen some awkward code that used dispatch_async to itself as a way of saying "as soon as I finish some series of actions, I then want to do something else". I've rarely seen this dispatching asynchronously to itself in situations where it couldn't be done more elegantly another way, but I have seen it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM