Re: Managing Multiple Connections
Re: Managing Multiple Connections
- Subject: Re: Managing Multiple Connections
- From: Becky Willrich <email@hidden>
- Date: Wed, 21 May 2003 09:03:46 -0700
I believe the select thread is the traditional way to do this in UNIX,
using a file descriptor for communication between other threads and the
select thread. As Quinn says, a thread per connection is dangerous if
you will have many open connections. If you can affectively limit the
number of connections to a handful, though, this approach is sound, and
is what several web servers do. On MacOS X, the easiest way to do what
you want, though, is to create a run loop source for each open
connection, then run the run loop. CFSocket will give you a run loop
source per socket; CFRead/WriteStream will give you a stream
abstraction (as well as run loop integration) per socket, and can be
used to build the socket for you if you wish. Your code will look
roughly like this:
for each socket
build a CFSocket or CFRead/WriteStream
if CFSocket
get CFSocket's run loop source
schedule the run loop source on the current run loop, current mode
else if CFRead/WriteStream
schedule the stream on the current run loop, current mode
CFRunLoopRun(CFRunLoopGetCurrent())
CFRunLoopRun() will never exit until the run loop is explicitly halted.
The usual way to arrange for cross-thread communication in this model
is to build a custom run loop source (see CFRunLoop.h; this isn't as
hard as it sounds) and install it on the run loop as well. When you
want to communicate something to the connection thread, you signal the
custom run loop source; that will cause its signal callback to be
called on the connection thread.
Hope that helps,
REW
On Wednesday, May 21, 2003, at 1:05 AM, Dietrich Epp wrote:
What is a good way to manage multiple connections (OSX)? Most of my
documentation is UNIX-specific, and my first thought was to create a
thread that manages all connections using the select(2) system call.
Unfortunately, this means that communication with the thread must
either be done using a timeout (ick), signals (double ick), or file
descriptors (ick squared). My second thought is to create a thread
for each connection, but this seems excessive.
I'm sure this situation is relatively common (e.g. downloading things
in a web browser) and I'm probably making things more complicated than
they should be.
Is there some good technique I'm missing? It would be great if it
were cross-platform, but OSX-specific is good too.
--Dietrich
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.