Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / September 2007



Tip: Looking for answers? Try searching our database.

does NSNotificationCenter block?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
William - 22 Sep 2007 05:28 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
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/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.