Wrapping my head around pthreads, nsthreads, and nsnotificationcenter

  • 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?
  • 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?
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today