I have an NSNotificationCenter used in a method where I start a thread.
This is done inside the following class method (I show the following
because it seems to be a pretty standard example for doing threads):
[NSThread detachNewThreadSelector:@selector(_connectWithPorts:)
toTarget:self withObject:portArray];
// ...
+ (void)_connectWithPorts:(NSArray *) portArray { ... }
And inside this method, once the thread is setup and complete, I make
the following call to post a notification to the main thread in my GUI
that this thread is now setup and ready to receive commands. My problem
is that I call the postNotificationName:: just before my run loop and
it appears to be blocking because none of the statements after the
postNotificationCenter:: are ever called.
nc = [NSNotificationCenter defaultCenter];
// ...
[nc postNotificationName:@"ServerSetupComplete" object:self];
NSLog(@"Never reaches this line");
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]];
} while ([serverObject isRunning]);
Anyone happen to have any ideas what's going on here?
Michael Ash - 22 Sep 2007 05:31 GMT
> I have an NSNotificationCenter used in a method where I start a thread.
> This is done inside the following class method (I show the following
[quoted text clipped - 16 lines]
> [nc postNotificationName:@"ServerSetupComplete" object:self];
> NSLog(@"Never reaches this line");
NSNotificationCenter is just a fancy form of message dispatch. When you
post a notification to the center, the center *immediately* notifies all
listeners, only returning control to the caller when it's done. As such,
not only will it block until all the other things are done, but it does
not do what you think it does: it does not "post a notification to the
main thread". Notifications are just fancy message sends, they know
nothing about threads.
You'll probably want to use performSelectorOnMainThread: for this.

Signature
Michael Ash
Rogue Amoeba Software
Tom Harrington - 23 Sep 2007 23:48 GMT
> I have an NSNotificationCenter used in a method where I start a thread.
> This is done inside the following class method (I show the following
[quoted text clipped - 23 lines]
>
> Anyone happen to have any ideas what's going on here?
If only there was something in the documentation that might explain
this. You'd think Apple would explain this kind of stuff somewhere.
Hey, it's right there in the NSNotificationCenter documentation!
"A notification center delivers notifications to observers
synchronously. In other words, the postNotification: methods do not
return until all observers have received and processed the notification."
Seems pretty clear, and pretty easy to find.
What's more:
"In a multithreaded application, notifications are always delivered in
the thread in which the notification was posted, which may not be the
same thread in which an observer registered itself."
So you can't use it to post a notification to a different thread either.

Signature
Tom "Tom" Harrington
MondoMouse makes your mouse mightier
See http://www.atomicbird.com/mondomouse/