Re: NSNotificationQueue & NSOperationQueue thread death
Re: NSNotificationQueue & NSOperationQueue thread death
- Subject: Re: NSNotificationQueue & NSOperationQueue thread death
- From: Michael Ash <email@hidden>
- Date: Wed, 6 May 2009 12:03:42 -0400
On Wed, May 6, 2009 at 11:49 AM, Jonathon Kuo
<email@hidden> wrote:
> On May 4, 2009, at 8:12 PM, Ken Thomases wrote Re: 'A couple NSRunLoop
> questions':
>
>> Every thread has exactly one run loop associated with it. You don't
>> create run loops nor do you remove them.
>
> On May 5, 2009, at 9:24 PM, Michael Ash wrote Re: 'NSNotificationQueue &
> NSOperationQueue thread death':
>
>> Background threads, whether directly managed by you or indirectly created
>> by NSOperationQueue, do not use a runloop by default.
>
> Sorry for my newb confusion. I know that these are different words from
> different authors, but I'm having trouble reconciling these two statements.
> How could a thread not have or use a runloop? Nothing on it would ever
> execute...?
That's a pretty good question.
It is correct that every thread conceptually has exactly one runloop.
I say "conceptually" because in fact they are created on demand. A
thread starts out with no runloop, but as soon something tries to
interact with that thread's runloop, one is created for it.
I think the root of your confusion stems from the role of the runloop.
A runloop is not a fundamental construct. It's basically an advanced
feature that goes on top of a thread. Consider this code:
[NSThread detachNewThreadSelector:@selector(thread:) toTarget:self
withObject:nil];
...
- (void)thread:(id)parameter { NSLog(@"Hello world!"); }
At no point in this code is a runloop ever involved. Execution of the
secondary thread goes straight to the -thread: method.
You *can* run a runloop on a thread if you want, by simply doing
something like this instead:
- (void)thread:(id)parameter { [[NSRunLoop currentRunLoop] run]; }
But it's up to you to make it happen. They're not shadowy objects
lurking in the background. Rather, they have to be used explicitly or
else they don't happen.
(And note that the main thread is no exception to this, even though it
may look that way. Look in your main.m file, at the call to
NSApplicationMain(), which starts your program running. That call does
a whole lot of work to get your program up and running, and one of the
things it does is start running the main thread's runloop. If you
leave out that call and start doing other things, no runloop.)
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden