On Wed, May 6, 2009 at 1:03 PM, Jonathon Kuo <newsli...@autonomy.caltech.edu> wrote: > On May 6, 2009, at 9:03 AM, Michael Ash wrote: > >> It is correct that every thread conceptually has exactly one runloop. >> I say "conceptually" because in fact they are created on demand. > > So if a thread has no need to pay attention to asynchronous events (timers, > input sources, etc) but is only in existence to run its own functional code > (say, calculate a square root), it has no need to have a runloop running. It > can simply execute in more or less linear fashion and exit.
Precisely correct. >> A thread starts out with no runloop, but as soon something tries to >> interact with that thread's runloop, one is created for it. > > Here's where I don't quite follow. If my thread doesn't set up and > explicitly run its runloop, won't async events just get ignored because the > thread has no code to receive/process them? Yes, they will. But that doesn't mean you can't interact with the runloop, only that if it never runs, it never processes what it contains. Imagine this method as the target of a new thread: - (void)thread:(id)parameter { [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timer:) userInfo:nil repeats:YES]; } NSTimer will access the current thread's runloop as part of scheduling the timer. Since this is the first time the runloop has been accessed, that will also cause it to be created. Then it returns, with the timer scheduled on the runloop. At that point, your method (and thus your thread) exits, and the timer never fires. But the runloop did exist, it just processes because you never ran it. Mike _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com