Re: What's better a separate run loop or separate thread?
Re: What's better a separate run loop or separate thread?
- Subject: Re: What's better a separate run loop or separate thread?
- From: Steve Sims <email@hidden>
- Date: Sat, 10 Jul 2004 11:17:13 -0400
On 10 Jul 2004, at 05:12, Ken Tozier wrote:
I have a number of processes that need to run constantly in the
background while my app does other things and I'm wondering if it
would be better to create dedicated run loops for them with NSTimers
for the required methods or to fork a thread with a timer.
Well I guess it depends on the nature of the code you need to run
constantly.
Now first up a disclaimer - I'm not an expert on threading, however I
believe what I'm about to say is correct...
If you use NSTimers then you'll have only one run loop, and thus only a
single thread. Within the context of your own application you will
therefore be co-operatively multi-tasking. There's nothing inherently
wrong with co-operative multi-tasking, but there are things that you
must bear in mind. The biggest issues are responsiveness and latency.
When co-operatively multi-tasking you need to be aware that your timers
won't necessarily fire off at the exact intervals you expect, since the
run loop of your application might be busy doing something else when
the timers are supposed to fire. Of course you can compensate for this
by checking the time.
Forking a new thread should mean that the timers are more likely to
fire off at the right time even if the rest of your application is
busy. I say more likely, since of course the rest of the OS could be
busy and slowing down execution.
Another thing to bear in mind is that there may be issues in talking to
the UI from another thread.
So as I see it your decision is mostly down to how accurately you need
your timers to fire.
Steve
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.