iPhone应用程序中带有NSTimers的“多线程”

时间:2022-09-23 21:00:22

Say I have two NSTimers in my iPhone app: timer1 and timer2. timer1 calls function1 30 times per second and timer2 calls function2 30 times per second. Assume these two functions are reading and updating the same integer variables. Are there any "multi-threading" issues here? If not how does iPhone OS handle the execution of the two functions (in general)?

假设我的iPhone应用程序中有两个NSTimers:timer1和timer2。 timer1每秒调用function1 30次,timer2每秒调用function2 30次。假设这两个函数正在读取和更新相同的整数变量。这里有“多线程”问题吗?如果不是,iPhone OS如何处理两个功能的执行(一般情况下)?

2 个解决方案

#1


5  

The core of any iPhone application (and some other platforms) is a run loop. Each thread may have a run loop, and the run loop on the main thread is set up for you. When there is something to do, like fire an NSTimer or draw the view hierarchy, the run loop performs those tasks. When there is nothing to do, the run loop is idle, allowing other things to process.

任何iPhone应用程序(以及其他一些平台)的核心都是运行循环。每个线程可能有一个运行循环,并为您设置主线程上的运行循环。当有事情要做时,比如触发NSTimer或绘制视图层次结构,运行循环执行这些任务。当无事可做时,运行循环处于空闲状态,允许其他事情处理。

The run loop internals are thread aware so that nothing handled by the run loop has to be. All the NSTimer callbacks and view rendering happens on a single thread in a serial or linear flow.

运行循环内部是线程感知的,因此运行循环不必处理任何内容。所有NSTimer回调和视图呈现都发生在串行或线性流中的单个线程上。

For specific details, you can look up NSRunLoop or CFRunLoop.

有关具体细节,您可以查找NSRunLoop或CFRunLoop。

#2


2  

As far as I'm aware, NSTimers use the run loop to achieve their 'asynchronosity'. If the timer is created using the scheduledTimerWith... method then it will be scheduled on the default (main) run loop and will be executed on the main thread.

据我所知,NSTimers使用run循环来实现“异步”。如果使用scheduledTimerWith ...方法创建计时器,那么它将在默认(主)运行循环上进行调度,并将在主线程上执行。

I don't think any new threads are created for the timer (unless you do it explicitly, and assign the timer to that run loop).

我不认为为计时器创建任何新线程(除非您明确地执行它,并将计时器分配给该运行循环)。

So in the end, I believe your two timers shouldn't conflict with each other.

所以最后,我相信你的两个计时器不应该相互冲突。

However, the documentation states that timers don't represent real time. The timer will fire on or after the scheduled time, but is not guaranteed to fire exactly at the scheduled time. Therefore if the fired method takes longer to execute than the interval of the timer, you may not see the method called as often as you'd expect.

但是,文档说明计时器并不代表实时。计时器将在预定时间或之后启动,但不保证在预定时间准确启动。因此,如果触发的方法执行的时间比计时器的间隔长,则可能无法按预期那样经常调用该方法。

With that said, is there any particular reason you're asking the question? Are you seeing undesired behaviour using this setup?

话虽如此,你问这个问题有什么特别的原因吗?您是否看到使用此设置的不良行为?

#1


5  

The core of any iPhone application (and some other platforms) is a run loop. Each thread may have a run loop, and the run loop on the main thread is set up for you. When there is something to do, like fire an NSTimer or draw the view hierarchy, the run loop performs those tasks. When there is nothing to do, the run loop is idle, allowing other things to process.

任何iPhone应用程序(以及其他一些平台)的核心都是运行循环。每个线程可能有一个运行循环,并为您设置主线程上的运行循环。当有事情要做时,比如触发NSTimer或绘制视图层次结构,运行循环执行这些任务。当无事可做时,运行循环处于空闲状态,允许其他事情处理。

The run loop internals are thread aware so that nothing handled by the run loop has to be. All the NSTimer callbacks and view rendering happens on a single thread in a serial or linear flow.

运行循环内部是线程感知的,因此运行循环不必处理任何内容。所有NSTimer回调和视图呈现都发生在串行或线性流中的单个线程上。

For specific details, you can look up NSRunLoop or CFRunLoop.

有关具体细节,您可以查找NSRunLoop或CFRunLoop。

#2


2  

As far as I'm aware, NSTimers use the run loop to achieve their 'asynchronosity'. If the timer is created using the scheduledTimerWith... method then it will be scheduled on the default (main) run loop and will be executed on the main thread.

据我所知,NSTimers使用run循环来实现“异步”。如果使用scheduledTimerWith ...方法创建计时器,那么它将在默认(主)运行循环上进行调度,并将在主线程上执行。

I don't think any new threads are created for the timer (unless you do it explicitly, and assign the timer to that run loop).

我不认为为计时器创建任何新线程(除非您明确地执行它,并将计时器分配给该运行循环)。

So in the end, I believe your two timers shouldn't conflict with each other.

所以最后,我相信你的两个计时器不应该相互冲突。

However, the documentation states that timers don't represent real time. The timer will fire on or after the scheduled time, but is not guaranteed to fire exactly at the scheduled time. Therefore if the fired method takes longer to execute than the interval of the timer, you may not see the method called as often as you'd expect.

但是,文档说明计时器并不代表实时。计时器将在预定时间或之后启动,但不保证在预定时间准确启动。因此,如果触发的方法执行的时间比计时器的间隔长,则可能无法按预期那样经常调用该方法。

With that said, is there any particular reason you're asking the question? Are you seeing undesired behaviour using this setup?

话虽如此,你问这个问题有什么特别的原因吗?您是否看到使用此设置的不良行为?