Re: Wrapping my head around pthreads, nsthreads, and nsnotificationcenter
Re: Wrapping my head around pthreads, nsthreads, and nsnotificationcenter
- Subject: Re: Wrapping my head around pthreads, nsthreads, and nsnotificationcenter
- From: Chris Kane <email@hidden>
- Date: Sat, 3 Nov 2007 08:40:58 -0700
NSThread is just an object layer on top of pthreads. There's a
pthread underneath every NSThread.
If you're having trouble receiving notifications, the problem is
unlikely to be thread-related. It's more likely to be a problem with
addObserver:... or removeObserver:... usage. For example, upon
receiving the first notification if the something is calling
removeObserver:..., it may be doing so specifying parameters that are
too broad and unintentionally wiping out other observers in the process.
People have a lot of confusion around notification and threads,
because they expect somehow there is a relationship between
notification observation and threads. Actually, it is usually that
they are hoping for that, so they can use notification posting as an
inter-thread communication mechanism. So it is not that
notifications "can only be received" on the thread from which they
are sent, it's that they simply are, because the posting algorithm
looks like this:
for each observer matching given name+object,
[observer performSelector:observersSelector
withObject:notification];
Chris Kane
Cocoa Frameworks, Apple
On Nov 2, 2007, at 12:28 PM, Ian Archer wrote:
I have an app that needs to remain portable across windows and unix,
although I'm trying to put Cocoa in whenever I can on the mac side.
pthreads are prevalent, although mac-specific stuff is all done in
nsthread.
Much of the mac code relies on NSNotificationCenter. I'm finding that
one of my objects is not receiving messages from the center (or, in
some cases, receives the first and receives no more).
I guessed this was due to either 1) NSNotificationCenter's thread
separation (e.g. messages can only be received on the thread from
which they are sent), or 2) some weird incompatibility between pthread
and nsthread.
In the object "A" which should be receiving messages but isn't, I
added a thread startup in its init method which basically does nothing
except spin and sleep. Now, this object is receiving messages from
the notificaton center! Does anyone know what might be causing this
strange behavior?
_______________________________________________
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
_______________________________________________
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