Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSThreads and BSD sockets



Hi,

I'm writing an application which listens to a socket. When a client
connects, the program will open a number of additional listening sockets,
spawning a thread for every opened port, like this:

Main thread
     |
     +-- master listen thread, socket port N (object A)
            |
            +-- listen thread port N + 1 (object B)
            +-- listen thread port N + 2 (object B)
            +-- listen thread port N + 3 (object B)
            +-- ....

The first listener thread is of object A, while the threads spawned by
object A are of type object B, however they both are subclasses of a
general object containing a socket object.
So, both object A and B use the same underlying code which is executed in
its own thread:

- set up nsconnection & nsport communication
- open a socket using the (custom) socket class
- run the runloop
- exit thread after runloop ends

The socket class opens a port and sets up the nessecary notifications. And
there's the problem; all threads seem to respond to incoming data on one
of the sockets (be it N, N + 1, ...).

In the master thread I temporarily removed all observers while the other
listen threads were created which, well, broke the socketconnection on the
master thread:

--
2005-06-09 13:38:38.135 NoiseXServer[2094] Exception raised during posting
of notification.  Ignored.  exception: *** -[NSConcreteFileHandle
performActivity:modes:]: a different activity is already in progress
--

The other threads happily continued responding to all other's messages;
thread on N+1 received notification of input on N+2 et cetera.

The code used to add observers, where RemoteHandle is the NSFileHandle
associated with the remote socket filedescriptor and self is the spawned
thread object:

--
  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

  [nc addObserver: self selector: @selector(Read:)
             name: NSFileHandleDataAvailableNotification
           object: RemoteHandle];
  [nc addObserver: self selector: @selector(Read:)
             name: NSFileHandleReadCompletionNotification
           object: RemoteHandle];
--

To check if the code responding to incoming data really lives in seperate
threads, it writes the thread ID on socket open; the IDs were different
for each socket.

I was under the impression that the notifications added in one thread,
with its own runloop, were not also received by another thread. Am I
correct, and doing something wrong, or is what I'm experiencing correct
behaviour?
Everything worked fine when I didn't use an NSRunLoop and notification
center in the threads, using a blocking read to receive data. However I
wanted to let the master talk to the spawned threads through an
NSConnection, which I presume needs an NSRunLoop to work.

Thanks for any pointers!


Kind regards,

-- Khamba Staring

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.