runloop如何实际工作 [英] How a runloop actually works

查看:128
本文介绍了runloop如何实际工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本月早些时候我问过这个问题'什么是runloop?'阅读完之后答案并做了一些尝试我让它工作,但我仍然不完全理解它。如果一个runloop只是一个与一个线程关联的循环,并且它不会在后台产生另一个线程,那么我的线程中的任何其他代码(mainthread保持简单)如何执行而不会被阻塞/不运行因为它在哪里进行无限循环?

Earlier this month I asked this question 'What is a runloop?' After reading the answers and did some tries I got it to work, but still I do not understand it completely. If a runloop is just an loop that is associated with an thread and it don't spawn another thread behind the scenes how can any of the other code in my thread(mainthread to keep it simple) execute without getting "blocked"/not run because it somewhere make an infinite loop?

这是第一个问题。然后到了我的第二个。

That was question number one. Then over to my second.

如果我在使用了这个之后得到了一些正确的东西,但是没有完全理解它是一个循环,你附加'标志'通知runloop,当它到达标志所在的点时,它会停止并执行该点附加的任何处理程序?然后继续运行到下一个。

If I got something right about this after having worked with this, but not completely understood it a runloop is a loop where you attach 'flags' that notify the runloop that when it comes to the point where the flag is, it "stops" and execute whatever handler that is attached at that point? Then afterwards it keep running to the next in que.

所以在这种情况下,没有事件放在连接中的que中,但是当它涉及事件时,它会采取与tap 1相关的任何操作并在它之前执行它再次运行到连接,等等。或者我可以理解这个概念吗?

So in this case no events is placed in que in connections, but when it comes to events it take whatever action associated with tap 1 and execute it before it runs to connections again and so on. Or am I as far as I can be from understanding the concept?

推荐答案

一个运行循环如果它调度一个花费太长时间或永远循环的方法就会被阻止。

A run loop will get blocked if it dispatches a method that takes too long or that loops forever.

这就是为什么iPhone应用程序想要做的所有事情都不适合1 UI运行循环的tick(比如某些动画帧速率或UI响应速率),以及需要在同一tick中完成的任何其他事件处理程序的空间,或者在调度时异步分解到另一个线程执行。

That's the reason why an iPhone app will want to do everything which won't fit into 1 "tick" of the UI run loop (say at some animation frame rate or UI response rate), and with room to spare for any other event handlers that need to be done in that same "tick", either broken up asynchronously, on dispatched to another thread for execution.

否则在控制返回到运行循环之前,东西会被阻塞。

Otherwise stuff will get blocked until control is returned to the run loop.

这篇关于runloop如何实际工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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